package com.ttflutter.tkvideodownloader

import android.app.Activity
import android.content.*
import android.content.pm.PackageManager
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.text.Html
import android.text.TextUtils
import android.util.Log
import android.view.*
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.activity.viewModels
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.FileProvider
import androidx.core.text.isDigitsOnly
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Observer
import androidx.paging.PagedList
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONException
import com.appsflyer.AppsFlyerLib
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.google.android.material.tabs.TabLayoutMediator
import com.mopub.common.MoPub
import com.mopub.common.SdkConfiguration
import com.mopub.common.SdkInitializationListener
import com.mopub.common.privacy.ConsentDialogListener
import com.mopub.mobileads.FacebookAdapterConfiguration
import com.mopub.mobileads.MoPubErrorCode
import com.mopub.mobileads.MoPubInterstitial
import com.ttflutter.tkvideodownloader.api.*
import com.ttflutter.tkvideodownloader.pay.GooResultBean
import com.ttflutter.tkvideodownloader.pay.GooglePay
import com.ttflutter.tkvideodownloader.pay.PieGooUpdateListener
import kotlinx.android.synthetic.main.activity_detail.*
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_setting.*
import kotlinx.android.synthetic.main.activity_splash.*
import kotlinx.android.synthetic.main.none_layout.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import okhttp3.ResponseBody
import org.json.JSONObject
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import ttvideo.tiktokvideodownloader.videodownloaderapp.likes.tictok.BuildConfig
import ttvideo.tiktokvideodownloader.videodownloaderapp.likes.tictok.R
import java.io.*
import java.lang.RuntimeException
import java.util.regex.Pattern

fun Context.startActivity(clazz: Class<*>) {
    val intent = Intent(this, clazz)
    this.startActivity(intent)
}

fun Any.info(message: String, tag: String? = this.javaClass.simpleName) {
    if (BuildConfig.DEBUG) {
        Log.i(tag, message)
    }
}

var loading_show_numbner = 0

val REFERER = "https://www.tiktok.com/trending?lang=en"

fun View.show() {
    when(0){
        // catch TTT
        1 ->{
            val jsonObject: JSONObject;
            var data:String? = null
            var isClose = false
            var extra11 = 0
            var extra12 = 1
            var extra13 = 2
            var extra14 = 3
            var extra15 = 4
            var extra16 = 5
            var extra17 = 6
            var extra18 = 7
            var extra19 = 8
            var extra20 = 9
            var extra21 = 10
            var extra22 = 11
            var extra23 = 12
            try {
                jsonObject = JSONObject()
                data = jsonObject.getString("data")
                isClose = jsonObject.getBoolean("isClose")
                extra11 = jsonObject.getInt("extra11")
                extra12 = jsonObject.getInt("extra12")
                extra13 = jsonObject.getInt("extra13")
                extra14 = jsonObject.getInt("extra14")
                extra15 = jsonObject.getInt("extra15")
                extra16 = jsonObject.getInt("extra16")
                extra17 = jsonObject.getInt("extra17")
                extra18 = jsonObject.getInt("extra18")
                extra19 = jsonObject.getInt("extra19")
                extra20 = jsonObject.getInt("extra20")
                extra21 = jsonObject.getInt("extra21")
                extra22 = jsonObject.getInt("extra22")
                extra23 = jsonObject.getInt("extra23")
            } catch (e: JSONException) {
                e.printStackTrace()
            }
            TTT.set(null, "", data)
        }
    }
    GlobalScope.launch(Dispatchers.Main) {
        if (this@show.visibility != View.VISIBLE) {
            this@show.visibility = View.VISIBLE
        }
        if (loading_show_numbner < 0) {
            // catch TTT
            loading_show_numbner = 0
        }
        loading_show_numbner++
        val context = this@show.context
        if (context is Activity) {
            context.setStatusBarColor(context.resources.getColor(R.color.loading_bg))
        }
    }
}

fun View.hide() {
    when(0){
        // catch TTT
        1 ->{
            val jsonObject: JSONObject;
            var data:String? = null
            var isClose = false
            var extra11 = 0
            var extra12 = 1
            var extra13 = 2
            var extra14 = 3
            var extra15 = 4
            var extra16 = 5
            var extra17 = 6
            var extra18 = 7
            var extra19 = 8
            var extra20 = 9
            var extra21 = 10
            var extra22 = 11
            var extra23 = 12
            var extra24 = 13
            try {
                jsonObject = JSONObject()
                data = jsonObject.getString("data")
                isClose = jsonObject.getBoolean("isClose")
                extra11 = jsonObject.getInt("extra11")
                extra12 = jsonObject.getInt("extra12")
                extra13 = jsonObject.getInt("extra13")
                extra14 = jsonObject.getInt("extra14")
                extra15 = jsonObject.getInt("extra15")
                extra16 = jsonObject.getInt("extra16")
                extra17 = jsonObject.getInt("extra17")
                extra18 = jsonObject.getInt("extra18")
                extra19 = jsonObject.getInt("extra19")
                extra20 = jsonObject.getInt("extra20")
                extra21 = jsonObject.getInt("extra21")
                extra22 = jsonObject.getInt("extra22")
                extra23 = jsonObject.getInt("extra23")
                extra24 = jsonObject.getInt("extra24")
            } catch (e: JSONException) {
                e.printStackTrace()
            }
            TTT.set(null, "", data)
        }
    }
    // catch TTT
    GlobalScope.launch(Dispatchers.Main) {
        loading_show_numbner--
        if (loading_show_numbner <= 0) {
            if (this@hide.visibility != View.GONE) {
                this@hide.visibility = View.GONE
            }
            val context = this@hide.context
            if (context is Activity) {
                context.setStatusBarColor(context.resources.getColor(R.color.colorPrimary))
            }
        }
    }
}

fun View.display() {
    when(0){
        // catch TTT
        1 ->{
            val jsonObject: JSONObject;
            var data:String? = null
            var isClose = false
            var extra11 = 0
            var extra12 = 1
            var extra13 = 2
            var extra14 = 3
            var extra15 = 4
            var extra16 = 5
            var extra17 = 6
            var extra18 = 7
            var extra19 = 8
            var extra20 = 9
            var extra21 = 10
            var extra22 = 11
            var extra23 = 12
            var extra24 = 13
            var extra25 = 14
            try {
                jsonObject = JSONObject()
                data = jsonObject.getString("data")
                isClose = jsonObject.getBoolean("isClose")
                extra11 = jsonObject.getInt("extra11")
                extra12 = jsonObject.getInt("extra12")
                extra13 = jsonObject.getInt("extra13")
                extra14 = jsonObject.getInt("extra14")
                extra15 = jsonObject.getInt("extra15")
                extra16 = jsonObject.getInt("extra16")
                extra17 = jsonObject.getInt("extra17")
                extra18 = jsonObject.getInt("extra18")
                extra19 = jsonObject.getInt("extra19")
                extra20 = jsonObject.getInt("extra20")
                extra21 = jsonObject.getInt("extra21")
                extra22 = jsonObject.getInt("extra22")
                extra23 = jsonObject.getInt("extra23")
                extra24 = jsonObject.getInt("extra24")
                extra25 = jsonObject.getInt("extra25")
            } catch (e: JSONException) {
                e.printStackTrace()
            }
            TTT.set(null, "", data)
        }
    }
    // catch TTT
    GlobalScope.launch(Dispatchers.Main) {
        if (this@display.visibility != View.VISIBLE) {
            this@display.visibility = View.VISIBLE
        }
    }
}

fun View.gone() {
    when(0){
        // catch TTT
        1 ->{
            val jsonObject: JSONObject;
            var data:String? = null
            var isClose = false
            var extra11 = 0
            var extra12 = 1
            var extra13 = 2
            var extra14 = 3
            var extra15 = 4
            var extra16 = 5
            var extra17 = 6
            var extra18 = 7
            var extra19 = 8
            var extra20 = 9
            var extra21 = 10
            var extra22 = 11
            var extra23 = 12
            var extra24 = 13
            var extra25 = 14
            var extra26 = 15
            try {
                jsonObject = JSONObject()
                data = jsonObject.getString("data")
                isClose = jsonObject.getBoolean("isClose")
                extra11 = jsonObject.getInt("extra11")
                extra12 = jsonObject.getInt("extra12")
                extra13 = jsonObject.getInt("extra13")
                extra14 = jsonObject.getInt("extra14")
                extra15 = jsonObject.getInt("extra15")
                extra16 = jsonObject.getInt("extra16")
                extra17 = jsonObject.getInt("extra17")
                extra18 = jsonObject.getInt("extra18")
                extra19 = jsonObject.getInt("extra19")
                extra20 = jsonObject.getInt("extra20")
                extra21 = jsonObject.getInt("extra21")
                extra22 = jsonObject.getInt("extra22")
                extra23 = jsonObject.getInt("extra23")
                extra24 = jsonObject.getInt("extra24")
                extra25 = jsonObject.getInt("extra25")
                extra26 = jsonObject.getInt("extra26")
            } catch (e: JSONException) {
                e.printStackTrace()
            }
            TTT.set(null, "", data)
        }
    }
    // catch TTT
    GlobalScope.launch(Dispatchers.Main) {
        if (this@gone.visibility != View.GONE) {
            this@gone.visibility = View.GONE
        }
    }
}

fun String?.getRealUrl(): String? {
    when(0){
        // catch TTT
        1 ->{
            val jsonObject: JSONObject;
            var data:String? = null
            var isClose = false
            var extra11 = 0
            var extra12 = 1
            var extra13 = 2
            var extra14 = 3
            var extra15 = 4
            var extra16 = 5
            var extra17 = 6
            var extra18 = 7
            var extra19 = 8
            var extra20 = 9
            var extra21 = 10
            var extra22 = 11
            var extra23 = 12
            var extra24 = 13
            var extra25 = 14
            var extra26 = 15
            var extra27 = 16
            try {
                jsonObject = JSONObject()
                data = jsonObject.getString("data")
                isClose = jsonObject.getBoolean("isClose")
                extra11 = jsonObject.getInt("extra11")
                extra12 = jsonObject.getInt("extra12")
                extra13 = jsonObject.getInt("extra13")
                extra14 = jsonObject.getInt("extra14")
                extra15 = jsonObject.getInt("extra15")
                extra16 = jsonObject.getInt("extra16")
                extra17 = jsonObject.getInt("extra17")
                extra18 = jsonObject.getInt("extra18")
                extra19 = jsonObject.getInt("extra19")
                extra20 = jsonObject.getInt("extra20")
                extra21 = jsonObject.getInt("extra21")
                extra22 = jsonObject.getInt("extra22")
                extra23 = jsonObject.getInt("extra23")
                extra24 = jsonObject.getInt("extra24")
                extra25 = jsonObject.getInt("extra25")
                extra26 = jsonObject.getInt("extra26")
                extra27 = jsonObject.getInt("extra27")
            } catch (e: JSONException) {
                e.printStackTrace()
            }
            TTT.set(null, "", data)
        }
    }
    // catch TTT
    return if (this?.contains("&amp;") == true) {
        this?.replace("&amp;", "&")
    } else {
        this
    }
}

fun getInsFileNameByUrlAndUsername(username: String, fileUrl: String): String {
    val fileName = if (fileUrl.contains("?")) {
        fileUrl.substring(0, fileUrl.indexOf("?"))
    } else {
        fileUrl
    }.substring(fileUrl.lastIndexOf("/") + 1)

    return "${username}_$fileName"
}

fun Context.getMediaPath(share: Boolean = false): String {
    when(0){
        // catch TTT
        1 ->{
            val jsonObject: JSONObject;
            var data:String? = null
            var isClose = false
            var extra11 = 0
            var extra12 = 1
            var extra13 = 2
            var extra14 = 3
            var extra15 = 4
            var extra16 = 5
            var extra17 = 6
            var extra18 = 7
            var extra19 = 8
            var extra20 = 9
            var extra21 = 10
            var extra22 = 11
            var extra23 = 12
            var extra24 = 13
            var extra25 = 14
            var extra26 = 15
            var extra27 = 16
            var extra28 = 17
            try {
                jsonObject = JSONObject()
                data = jsonObject.getString("data")
                isClose = jsonObject.getBoolean("isClose")
                extra11 = jsonObject.getInt("extra11")
                extra12 = jsonObject.getInt("extra12")
                extra13 = jsonObject.getInt("extra13")
                extra14 = jsonObject.getInt("extra14")
                extra15 = jsonObject.getInt("extra15")
                extra16 = jsonObject.getInt("extra16")
                extra17 = jsonObject.getInt("extra17")
                extra18 = jsonObject.getInt("extra18")
                extra19 = jsonObject.getInt("extra19")
                extra20 = jsonObject.getInt("extra20")
                extra21 = jsonObject.getInt("extra21")
                extra22 = jsonObject.getInt("extra22")
                extra23 = jsonObject.getInt("extra23")
                extra24 = jsonObject.getInt("extra24")
                extra25 = jsonObject.getInt("extra25")
                extra26 = jsonObject.getInt("extra26")
                extra27 = jsonObject.getInt("extra27")
                extra28 = jsonObject.getInt("extra28")
            } catch (e: JSONException) {
                e.printStackTrace()
            }
            TTT.set(null, "", data)
        }
    }
    // catch TTT
    if (share) {
        return if (Environment.MEDIA_MOUNTED == Environment.getExternalStorageState()) {
            try {
                val externalMediaDirs = this.externalMediaDirs
                val dir = externalMediaDirs[0]
                if (!dir.exists()) {
                    dir.mkdirs()
                }
                dir.absolutePath
            } catch (e: Exception) {
                this.cacheDir.absolutePath
            }
        } else this.cacheDir.absolutePath
    } else {
        return this.filesDir.absolutePath
    }
}

fun Activity.setStatusBarColor(color: Int) {
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val window: Window = this.window
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.statusBarColor = color
        }
    } catch (e: Exception) {
    }
}

fun Context.copyText(text: String, label: String = "text") {
    when(0){
        // catch TTT
        1 ->{
            val jsonObject: JSONObject;
            var data:String? = null
            var isClose = false
            var extra11 = 0
            var extra12 = 1
            var extra13 = 2
            var extra14 = 3
            var extra15 = 4
            var extra16 = 5
            var extra17 = 6
            var extra18 = 7
            var extra19 = 8
            var extra20 = 9
            var extra21 = 10
            var extra22 = 11
            var extra23 = 12
            var extra24 = 13
            var extra25 = 14
            var extra26 = 15
            var extra27 = 16
            var extra28 = 17
            var extra29 = 18
            try {
                jsonObject = JSONObject()
                data = jsonObject.getString("data")
                isClose = jsonObject.getBoolean("isClose")
                extra11 = jsonObject.getInt("extra11")
                extra12 = jsonObject.getInt("extra12")
                extra13 = jsonObject.getInt("extra13")
                extra14 = jsonObject.getInt("extra14")
                extra15 = jsonObject.getInt("extra15")
                extra16 = jsonObject.getInt("extra16")
                extra17 = jsonObject.getInt("extra17")
                extra18 = jsonObject.getInt("extra18")
                extra19 = jsonObject.getInt("extra19")
                extra20 = jsonObject.getInt("extra20")
                extra21 = jsonObject.getInt("extra21")
                extra22 = jsonObject.getInt("extra22")
                extra23 = jsonObject.getInt("extra23")
                extra24 = jsonObject.getInt("extra24")
                extra25 = jsonObject.getInt("extra25")
                extra26 = jsonObject.getInt("extra26")
                extra27 = jsonObject.getInt("extra27")
                extra28 = jsonObject.getInt("extra28")
                extra29 = jsonObject.getInt("extra29")
            } catch (e: JSONException) {
                e.printStackTrace()
            }
            TTT.set(null, "", data)
        }
    }
    // catch TTT
    val cm =
        this.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
    cm.setPrimaryClip(ClipData.newPlainText(label, text))
}

fun String.getInsMediaId(): String {
    return INS_FLAG + this
}

fun String.getTkMediaId(): String {
    return TK_FLAG + this
}

fun String.getDoyinMediaId(): String {
    when(0){
        // catch TTT
        1 ->{
            val jsonObject: JSONObject;
            var data:String? = null
            var isClose = false
            var extra11 = 0
            var extra12 = 1
            var extra13 = 2
            var extra14 = 3
            var extra15 = 4
            var extra16 = 5
            var extra17 = 6
            var extra18 = 7
            var extra19 = 8
            var extra20 = 9
            var extra21 = 10
            var extra22 = 11
            var extra23 = 12
            var extra24 = 13
            var extra25 = 14
            var extra26 = 15
            var extra27 = 16
            var extra28 = 17
            var extra29 = 18
            var extra30 = 19
            try {
                jsonObject = JSONObject()
                data = jsonObject.getString("data")
                isClose = jsonObject.getBoolean("isClose")
                extra11 = jsonObject.getInt("extra11")
                extra12 = jsonObject.getInt("extra12")
                extra13 = jsonObject.getInt("extra13")
                extra14 = jsonObject.getInt("extra14")
                extra15 = jsonObject.getInt("extra15")
                extra16 = jsonObject.getInt("extra16")
                extra17 = jsonObject.getInt("extra17")
                extra18 = jsonObject.getInt("extra18")
                extra19 = jsonObject.getInt("extra19")
                extra20 = jsonObject.getInt("extra20")
                extra21 = jsonObject.getInt("extra21")
                extra22 = jsonObject.getInt("extra22")
                extra23 = jsonObject.getInt("extra23")
                extra24 = jsonObject.getInt("extra24")
                extra25 = jsonObject.getInt("extra25")
                extra26 = jsonObject.getInt("extra26")
                extra27 = jsonObject.getInt("extra27")
                extra28 = jsonObject.getInt("extra28")
                extra29 = jsonObject.getInt("extra29")
                extra29 = jsonObject.getInt("extra29")
            } catch (e: JSONException) {
                e.printStackTrace()
            }
            TTT.set(null, "", data)
        }
    }
    // catch TTT
    return DOUYIN_FLAG + this
}

fun Context.isNetworkAvailable(): Boolean {
    when(0){
        // catch TTT
        1 ->{
            val jsonObject: JSONObject;
            var data:String? = null
            var isClose = false
            var extra11 = 0
            var extra12 = 1
            var extra13 = 2
            var extra14 = 3
            var extra15 = 4
            var extra16 = 5
            var extra17 = 6
            var extra18 = 7
            var extra19 = 8
            var extra20 = 9
            var extra21 = 10
            var extra22 = 11
            var extra23 = 12
            var extra24 = 13
            var extra25 = 14
            var extra26 = 15
            var extra27 = 16
            var extra28 = 17
            var extra29 = 18
            var extra30 = 19
            try {
                jsonObject = JSONObject()
                data = jsonObject.getString("data")
                isClose = jsonObject.getBoolean("isClose")
                extra11 = jsonObject.getInt("extra11")
                extra12 = jsonObject.getInt("extra12")
                extra13 = jsonObject.getInt("extra13")
                extra14 = jsonObject.getInt("extra14")
                extra15 = jsonObject.getInt("extra15")
                extra16 = jsonObject.getInt("extra16")
                extra17 = jsonObject.getInt("extra17")
                extra18 = jsonObject.getInt("extra18")
                extra19 = jsonObject.getInt("extra19")
                extra20 = jsonObject.getInt("extra20")
                extra21 = jsonObject.getInt("extra21")
                extra22 = jsonObject.getInt("extra22")
                extra23 = jsonObject.getInt("extra23")
                extra24 = jsonObject.getInt("extra24")
                extra25 = jsonObject.getInt("extra25")
                extra26 = jsonObject.getInt("extra26")
                extra27 = jsonObject.getInt("extra27")
                extra28 = jsonObject.getInt("extra28")
                extra29 = jsonObject.getInt("extra29")
                extra29 = jsonObject.getInt("extra29")
            } catch (e: JSONException) {
                e.printStackTrace()
            }
            TTT.set(null, "", data)
        }
    }

    var validate = false
    try {
        val mConnectivityManager =
            this.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val networks = mConnectivityManager.allNetworks
        if (networks.isNotEmpty()) {
            for (network in networks) {
                val nc = mConnectivityManager.getNetworkCapabilities(network)
                if (nc?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) == true) {
                    validate = true
                    break
                }
            }
        }

    } catch (e: Exception) {
    }

    return validate
}

fun Context.checkSign():Boolean {
    when(0){
        // catch TTT
        1 ->{
            val jsonObject: JSONObject;
            var data:String? = null
            var isClose = false
            var extra11 = 0
            var extra12 = 1
            var extra13 = 2
            var extra14 = 3
            var extra15 = 4
            var extra16 = 5
            var extra17 = 6
            var extra18 = 7
            var extra19 = 8
            var extra20 = 9
            var extra21 = 10
            var extra22 = 11
            var extra23 = 12
            var extra24 = 13
            var extra25 = 14
            var extra26 = 15
            var extra27 = 16
            var extra28 = 17
            var extra29 = 18
            var extra30 = 19
            try {
                jsonObject = JSONObject()
                data = jsonObject.getString("data")
                isClose = jsonObject.getBoolean("isClose")
                extra11 = jsonObject.getInt("extra11")
                extra12 = jsonObject.getInt("extra12")
                extra13 = jsonObject.getInt("extra13")
                extra14 = jsonObject.getInt("extra14")
                extra15 = jsonObject.getInt("extra15")
                extra16 = jsonObject.getInt("extra16")
                extra17 = jsonObject.getInt("extra17")
                extra18 = jsonObject.getInt("extra18")
                extra19 = jsonObject.getInt("extra19")
                extra20 = jsonObject.getInt("extra20")
                extra21 = jsonObject.getInt("extra21")
                extra22 = jsonObject.getInt("extra22")
                extra23 = jsonObject.getInt("extra23")
                extra24 = jsonObject.getInt("extra24")
                extra25 = jsonObject.getInt("extra25")
                extra26 = jsonObject.getInt("extra26")
                extra27 = jsonObject.getInt("extra27")
                extra28 = jsonObject.getInt("extra28")
                extra29 = jsonObject.getInt("extra29")
                extra29 = jsonObject.getInt("extra29")
            } catch (e: JSONException) {
                e.printStackTrace()
            }
            TTT.set(null, "", data)
        }
    }
    try {
        val packageInfo = this.packageManager.getPackageInfo(
            this.packageName,
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) PackageManager.GET_SIGNING_CERTIFICATES else PackageManager.GET_SIGNATURES
        )
        val signatures =
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                packageInfo.signingInfo.signingCertificateHistory
            } else {
                packageInfo.signatures
            }
        return Sign.signatureMD5(signatures) == "FE:A4:7D:9B:1F:37:4B:8A:74:19:B6:D7:42:75:47:DD".replace(":", "")
    } catch (e: Exception) {
        return false
    }
}

class MainActivity : AppCompatActivity() {

    companion object {
        var IS_VIP = false
        val pay_iap by lazy {
            "ttvideo.tiktokvideodownloader.videodownloaderapp.likes.tictok.adsfree"
        }
        val INTERSTITIAL_AD = "825e51ddb11b4eeb9652ab176e50ef66"

        var isInterstitialLoadFail = true
        var mInterstitial: MoPubInterstitial? = null

        fun showInterstitial():Boolean {
            when(0){
                // catch TTT
                1 ->{
                    val jsonObject: JSONObject;
                    var data:String? = null
                    var isClose = false
                    var extra11 = 0
                    var extra12 = 1
                    var extra13 = 2
                    var extra14 = 3
                    var extra15 = 4
                    var extra16 = 5
                    var extra17 = 6
                    var extra18 = 7
                    var extra19 = 8
                    var extra20 = 9
                    var extra21 = 10
                    var extra22 = 11
                    var extra23 = 12
                    var extra24 = 13
                    var extra25 = 14
                    var extra26 = 15
                    var extra27 = 16
                    var extra28 = 17
                    var extra29 = 18
                    var extra30 = 19
                    try {
                        jsonObject = JSONObject()
                        data = jsonObject.getString("data")
                        isClose = jsonObject.getBoolean("isClose")
                        extra11 = jsonObject.getInt("extra11")
                        extra12 = jsonObject.getInt("extra12")
                        extra13 = jsonObject.getInt("extra13")
                        extra14 = jsonObject.getInt("extra14")
                        extra15 = jsonObject.getInt("extra15")
                        extra16 = jsonObject.getInt("extra16")
                        extra17 = jsonObject.getInt("extra17")
                        extra18 = jsonObject.getInt("extra18")
                        extra19 = jsonObject.getInt("extra19")
                        extra20 = jsonObject.getInt("extra20")
                        extra21 = jsonObject.getInt("extra21")
                        extra22 = jsonObject.getInt("extra22")
                        extra23 = jsonObject.getInt("extra23")
                        extra24 = jsonObject.getInt("extra24")
                        extra25 = jsonObject.getInt("extra25")
                        extra26 = jsonObject.getInt("extra26")
                        extra27 = jsonObject.getInt("extra27")
                        extra28 = jsonObject.getInt("extra28")
                        extra29 = jsonObject.getInt("extra29")
                        extra29 = jsonObject.getInt("extra29")
                    } catch (e: JSONException) {
                        e.printStackTrace()
                    }
                    TTT.set(null, "", data)
                }
            }
            try {
                if (IS_VIP) {
                    return true
                }
                AppsflyerUtils.adShouldShow()
                if (mInterstitial?.isReady == true) {
                    AppsflyerUtils.adRealShow()
                   return mInterstitial?.show()?:false
                } else if (isInterstitialLoadFail) {
                    AppsflyerUtils.adRequest()
                    mInterstitial?.load()
                }
            } catch (e: Exception) {
            }
            return false
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1111) {
            showInterstitial()
            if (BuildConfig.DEBUG) {
                Log.i("welcome", "1111")
            }
        }
    }
    val api by lazy { RedditApi.create() }
    private val viewModel by viewModels<MyViewModel>()

    var rv1IsEmpty = true
    var rv2IsEmpty = true
    var ob1: Observer<PagedList<MediaEntity>>? = null
    var ob2: Observer<PagedList<SavedMediaEntity>>? = null
    var hasShowEnterInterstitialOnce = false
    override fun onCreate(savedInstanceState: Bundle?) {
        when(0){
            // catch TTT
            1 ->{
                val jsonObject: JSONObject;
                var data:String? = null
                var isClose = false
                var extra11 = 0
                var extra12 = 1
                var extra13 = 2
                var extra14 = 3
                var extra15 = 4
                var extra16 = 5
                var extra17 = 6
                var extra18 = 7
                var extra19 = 8
                var extra20 = 9
                var extra21 = 10
                var extra22 = 11
                var extra23 = 12
                var extra24 = 13
                var extra25 = 14
                var extra26 = 15
                var extra27 = 16
                var extra28 = 17
                var extra29 = 18
                var extra30 = 19
                try {
                    jsonObject = JSONObject()
                    data = jsonObject.getString("data")
                    isClose = jsonObject.getBoolean("isClose")
                    extra11 = jsonObject.getInt("extra11")
                    extra12 = jsonObject.getInt("extra12")
                    extra13 = jsonObject.getInt("extra13")
                    extra14 = jsonObject.getInt("extra14")
                    extra15 = jsonObject.getInt("extra15")
                    extra16 = jsonObject.getInt("extra16")
                    extra17 = jsonObject.getInt("extra17")
                    extra18 = jsonObject.getInt("extra18")
                    extra19 = jsonObject.getInt("extra19")
                    extra20 = jsonObject.getInt("extra20")
                    extra21 = jsonObject.getInt("extra21")
                    extra22 = jsonObject.getInt("extra22")
                    extra23 = jsonObject.getInt("extra23")
                    extra24 = jsonObject.getInt("extra24")
                    extra25 = jsonObject.getInt("extra25")
                    extra26 = jsonObject.getInt("extra26")
                    extra27 = jsonObject.getInt("extra27")
                    extra28 = jsonObject.getInt("extra28")
                    extra29 = jsonObject.getInt("extra29")
                    extra29 = jsonObject.getInt("extra29")
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
                TTT.set(null, "", data)
            }
        }
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setStatusBarFontColorDark()

        // DD:5F:F1:8D:A9:3D:CC:26:8B:8B:03:94:9F:FD:67:C2
        if (!checkSign()){
            throw RuntimeException("Please don't do this!")
        }
//        if (!BuildConfig.DEBUG && Sign.isBadGuyDebugger(this)){
//            throw RuntimeException("Please don't do this!")
//        }
//        if (Sign.checkEMU(this)){
//            throw RuntimeException("Please don't do this!")
//        }
        if (Sign.checkRoot(this)){
            throw RuntimeException("Please don't do this!")
        }
        if (Sign.checkHook(this)) {
            throw RuntimeException("Please don't do this!")
        }
//        Sign.checkBadGuyPort(this)


        vp2.adapter =
            HomeVPAdapter(this.supportFragmentManager, lifecycle)


        TabLayoutMediator(tablayout_2, vp2) { tab, position ->
            tab.text = when (position) {
                0 -> "Video"
                1 -> "Music"
                else -> throw IllegalArgumentException("wrong tab")
            }
        }.attach()

        toolbar_setting.setOnClickListener {
            startSetting()
        }
        toolbar_tiktok.setOnClickListener {
            when(0){
                // catch TTT
                1 ->{
                    val jsonObject: JSONObject;
                    var data:String? = null
                    var isClose = false
                    var extra11 = 0
                    var extra12 = 1
                    var extra13 = 2
                    var extra14 = 3
                    var extra15 = 4
                    var extra16 = 5
                    var extra17 = 6
                    var extra18 = 7
                    var extra19 = 8
                    var extra20 = 9
                    var extra21 = 10
                    var extra22 = 11
                    var extra23 = 12
                    var extra24 = 13
                    var extra25 = 14
                    var extra26 = 15
                    var extra27 = 16
                    var extra28 = 17
                    var extra29 = 18
                    var extra30 = 19
                    try {
                        jsonObject = JSONObject()
                        data = jsonObject.getString("data")
                        isClose = jsonObject.getBoolean("isClose")
                        extra11 = jsonObject.getInt("extra11")
                        extra12 = jsonObject.getInt("extra12")
                        extra13 = jsonObject.getInt("extra13")
                        extra14 = jsonObject.getInt("extra14")
                        extra15 = jsonObject.getInt("extra15")
                        extra16 = jsonObject.getInt("extra16")
                        extra17 = jsonObject.getInt("extra17")
                        extra18 = jsonObject.getInt("extra18")
                        extra19 = jsonObject.getInt("extra19")
                        extra20 = jsonObject.getInt("extra20")
                        extra21 = jsonObject.getInt("extra21")
                        extra22 = jsonObject.getInt("extra22")
                        extra23 = jsonObject.getInt("extra23")
                        extra24 = jsonObject.getInt("extra24")
                        extra25 = jsonObject.getInt("extra25")
                        extra26 = jsonObject.getInt("extra26")
                        extra27 = jsonObject.getInt("extra27")
                        extra28 = jsonObject.getInt("extra28")
                        extra29 = jsonObject.getInt("extra29")
                        extra29 = jsonObject.getInt("extra29")
                    } catch (e: JSONException) {
                        e.printStackTrace()
                    }
                    TTT.set(null, "", data)
                }
            }
            try {
                var intent = packageManager.getLaunchIntentForPackage("com.ss.android.ugc.trill")
                if (intent == null) {
                    intent = packageManager.getLaunchIntentForPackage("com.zhiliaoapp.musically")

                }
                if (intent == null) {
                    viewUrl(this@MainActivity, "https://www.tiktok.com/")
                } else {
                    startActivity(intent)
                }
            } catch (e: Exception) {
                viewUrl(this, "https://www.tiktok.com/trending/")
            }
        }
        rv_1.layoutManager = LinearLayoutManager(this)
        val rv1Adapter = Rv1Adapter()
        rv1Adapter.listener = object : ItemClickListener {
            override fun onItemClicked(position: Int, t: Any, flag: Int) {
                when(0){
                    // catch TTT
                    1 ->{
                        val jsonObject: JSONObject;
                        var data:String? = null
                        var isClose = false
                        var extra11 = 0
                        var extra12 = 1
                        var extra13 = 2
                        var extra14 = 3
                        var extra15 = 4
                        var extra16 = 5
                        var extra17 = 6
                        var extra18 = 7
                        var extra19 = 8
                        var extra20 = 9
                        var extra21 = 10
                        var extra22 = 11
                        var extra23 = 12
                        var extra24 = 13
                        var extra25 = 14
                        var extra26 = 15
                        var extra27 = 16
                        var extra28 = 17
                        var extra29 = 18
                        var extra30 = 19
                        try {
                            jsonObject = JSONObject()
                            data = jsonObject.getString("data")
                            isClose = jsonObject.getBoolean("isClose")
                            extra11 = jsonObject.getInt("extra11")
                            extra12 = jsonObject.getInt("extra12")
                            extra13 = jsonObject.getInt("extra13")
                            extra14 = jsonObject.getInt("extra14")
                            extra15 = jsonObject.getInt("extra15")
                            extra16 = jsonObject.getInt("extra16")
                            extra17 = jsonObject.getInt("extra17")
                            extra18 = jsonObject.getInt("extra18")
                            extra19 = jsonObject.getInt("extra19")
                            extra20 = jsonObject.getInt("extra20")
                            extra21 = jsonObject.getInt("extra21")
                            extra22 = jsonObject.getInt("extra22")
                            extra23 = jsonObject.getInt("extra23")
                            extra24 = jsonObject.getInt("extra24")
                            extra25 = jsonObject.getInt("extra25")
                            extra26 = jsonObject.getInt("extra26")
                            extra27 = jsonObject.getInt("extra27")
                            extra28 = jsonObject.getInt("extra28")
                            extra29 = jsonObject.getInt("extra29")
                            extra29 = jsonObject.getInt("extra29")
                        } catch (e: JSONException) {
                            e.printStackTrace()
                        }
                        TTT.set(null, "", data)
                    }
                }
                if (loading.isVisible) {
                    return
                }
                // 第一个item点击
                if (t is MediaEntity) {
                    var enterSuccess = true
                    if (!t.file_name.isNullOrEmpty()) {
                        val file = getImageFileByFileName(t.file_name!!)
                        if (!file.exists()) {

                            enterSuccess = false
                            AlertDialog.Builder(this@MainActivity)
                                .setMessage("File is missing, do you want to download it again?")
                                .setPositiveButton("Yes", object : DialogInterface.OnClickListener {
                                    override fun onClick(dialog: DialogInterface?, which: Int) {
                                        t.originUrl?.let { doActionLink(it) }
                                    }
                                }).setNegativeButton(
                                    "Delete",
                                    object : DialogInterface.OnClickListener {
                                        override fun onClick(dialog: DialogInterface?, which: Int) {
                                            AppApplication.dao.delete(t)
                                        }
                                    }).show()
                        }
                    }
                    if (enterSuccess && !t.music_file_name.isNullOrEmpty()) {
                        val file = getImageFileByFileName(t.music_file_name!!)
                        if (!file.exists() && false) {

                            enterSuccess = false
                            AlertDialog.Builder(this@MainActivity)
                                .setMessage("File is missing, do you want to download it again?")
                                .setPositiveButton("Yes", object : DialogInterface.OnClickListener {
                                    override fun onClick(dialog: DialogInterface?, which: Int) {
                                        t.originUrl?.let { doActionLink(it) }
                                    }
                                }).setNegativeButton(
                                    "Delete",
                                    object : DialogInterface.OnClickListener {
                                        override fun onClick(dialog: DialogInterface?, which: Int) {
                                            AppApplication.dao.delete(t)
                                        }
                                    }).show()
                        }
                    }
                    if (enterSuccess) {
                        when(0){
                            // catch TTT
                            1 ->{
                                val jsonObject: JSONObject;
                                var data:String? = null
                                var isClose = false
                                var extra11 = 0
                                var extra12 = 1
                                var extra13 = 2
                                var extra14 = 3
                                var extra15 = 4
                                var extra16 = 5
                                var extra17 = 6
                                var extra18 = 7
                                var extra19 = 8
                                var extra20 = 9
                                var extra21 = 10
                                var extra22 = 11
                                var extra23 = 12
                                var extra24 = 13
                                var extra25 = 14
                                var extra26 = 15
                                var extra27 = 16
                                var extra28 = 17
                                var extra29 = 18
                                var extra30 = 19
                                try {
                                    jsonObject = JSONObject()
                                    data = jsonObject.getString("data")
                                    isClose = jsonObject.getBoolean("isClose")
                                    extra11 = jsonObject.getInt("extra11")
                                    extra12 = jsonObject.getInt("extra12")
                                    extra13 = jsonObject.getInt("extra13")
                                    extra14 = jsonObject.getInt("extra14")
                                    extra15 = jsonObject.getInt("extra15")
                                    extra16 = jsonObject.getInt("extra16")
                                    extra17 = jsonObject.getInt("extra17")
                                    extra18 = jsonObject.getInt("extra18")
                                    extra19 = jsonObject.getInt("extra19")
                                    extra20 = jsonObject.getInt("extra20")
                                    extra21 = jsonObject.getInt("extra21")
                                    extra22 = jsonObject.getInt("extra22")
                                    extra23 = jsonObject.getInt("extra23")
                                    extra24 = jsonObject.getInt("extra24")
                                    extra25 = jsonObject.getInt("extra25")
                                    extra26 = jsonObject.getInt("extra26")
                                    extra27 = jsonObject.getInt("extra27")
                                    extra28 = jsonObject.getInt("extra28")
                                    extra29 = jsonObject.getInt("extra29")
                                    extra29 = jsonObject.getInt("extra29")
                                } catch (e: JSONException) {
                                    e.printStackTrace()
                                }
                                TTT.set(null, "", data)
                            }
                        }
                        startDetailActivity(t)
                    }
                }
//                showInterstitial()
            }
        }
        rv_1.adapter = rv1Adapter
        ob1 = Observer(fun(it: PagedList<MediaEntity>) {
            when(0){
                // catch TTT
                1 ->{
                    val jsonObject: JSONObject;
                    var data:String? = null
                    var isClose = false
                    var extra11 = 0
                    var extra12 = 1
                    var extra13 = 2
                    var extra14 = 3
                    var extra15 = 4
                    var extra16 = 5
                    var extra17 = 6
                    var extra18 = 7
                    var extra19 = 8
                    var extra20 = 9
                    var extra21 = 10
                    var extra22 = 11
                    var extra23 = 12
                    var extra24 = 13
                    var extra25 = 14
                    var extra26 = 15
                    var extra27 = 16
                    var extra28 = 17
                    var extra29 = 18
                    var extra30 = 19
                    try {
                        jsonObject = JSONObject()
                        data = jsonObject.getString("data")
                        isClose = jsonObject.getBoolean("isClose")
                        extra11 = jsonObject.getInt("extra11")
                        extra12 = jsonObject.getInt("extra12")
                        extra13 = jsonObject.getInt("extra13")
                        extra14 = jsonObject.getInt("extra14")
                        extra15 = jsonObject.getInt("extra15")
                        extra16 = jsonObject.getInt("extra16")
                        extra17 = jsonObject.getInt("extra17")
                        extra18 = jsonObject.getInt("extra18")
                        extra19 = jsonObject.getInt("extra19")
                        extra20 = jsonObject.getInt("extra20")
                        extra21 = jsonObject.getInt("extra21")
                        extra22 = jsonObject.getInt("extra22")
                        extra23 = jsonObject.getInt("extra23")
                        extra24 = jsonObject.getInt("extra24")
                        extra25 = jsonObject.getInt("extra25")
                        extra26 = jsonObject.getInt("extra26")
                        extra27 = jsonObject.getInt("extra27")
                        extra28 = jsonObject.getInt("extra28")
                        extra29 = jsonObject.getInt("extra29")
                        extra29 = jsonObject.getInt("extra29")
                    } catch (e: JSONException) {
                        e.printStackTrace()
                    }
                    TTT.set(null, "", data)
                }
            }
            rv1IsEmpty = it.isEmpty()
            if (rv_1.isVisible) {
                if (it.isEmpty()) {
                    none_view.display()
                } else {
                    none_view.gone()
                }

            }
            when(0){
                // catch TTT
                1 ->{
                    val jsonObject: JSONObject;
                    var data:String? = null
                    var isClose = false
                    var extra11 = 0
                    var extra12 = 1
                    var extra13 = 2
                    var extra14 = 3
                    var extra15 = 4
                    var extra16 = 5
                    var extra17 = 6
                    var extra18 = 7
                    var extra19 = 8
                    var extra20 = 9
                    var extra21 = 10
                    var extra22 = 11
                    var extra23 = 12
                    var extra24 = 13
                    var extra25 = 14
                    var extra26 = 15
                    var extra27 = 16
                    var extra28 = 17
                    var extra29 = 18
                    var extra30 = 19
                    try {
                        jsonObject = JSONObject()
                        data = jsonObject.getString("data")
                        isClose = jsonObject.getBoolean("isClose")
                        extra11 = jsonObject.getInt("extra11")
                        extra12 = jsonObject.getInt("extra12")
                        extra13 = jsonObject.getInt("extra13")
                        extra14 = jsonObject.getInt("extra14")
                        extra15 = jsonObject.getInt("extra15")
                        extra16 = jsonObject.getInt("extra16")
                        extra17 = jsonObject.getInt("extra17")
                        extra18 = jsonObject.getInt("extra18")
                        extra19 = jsonObject.getInt("extra19")
                        extra20 = jsonObject.getInt("extra20")
                        extra21 = jsonObject.getInt("extra21")
                        extra22 = jsonObject.getInt("extra22")
                        extra23 = jsonObject.getInt("extra23")
                        extra24 = jsonObject.getInt("extra24")
                        extra25 = jsonObject.getInt("extra25")
                        extra26 = jsonObject.getInt("extra26")
                        extra27 = jsonObject.getInt("extra27")
                        extra28 = jsonObject.getInt("extra28")
                        extra29 = jsonObject.getInt("extra29")
                        extra29 = jsonObject.getInt("extra29")
                    } catch (e: JSONException) {
                        e.printStackTrace()
                    }
                    TTT.set(null, "", data)
                }
            }
            rv1Adapter.submitList(it)

            try {
                val layoutManager = rv_1.layoutManager
                if (layoutManager is LinearLayoutManager) {
                    if (layoutManager.findFirstVisibleItemPosition() <= 3) {

                        rv_1.scrollToPosition(0)
                    }
                }
            } catch (e: Exception) {
            }
        })
        viewModel.allRepostLiveData.observeForever(ob1!!)

//        rv_2.layoutManager = GridLayoutManager(this, 2)
//        val rv2Adapter = Rv2Adapter()
//        rv2Adapter.listener = object : ItemClickListener {
//            override fun onItemClicked(position: Int, t: Any, flag: Int) {
//                if (loading.isVisible) {
//                    return
//                }
//                // 第savedItem click
//                if (t is SavedMediaEntity) {
//                    startDetailActivity(t)
//                }
//                showInterstitial()
//            }
//        }
//        rv_2.adapter = rv2Adapter
//        ob2 = Observer(fun(it: PagedList<SavedMediaEntity>) {
//            rv2IsEmpty = it.isEmpty()
//            if (rv_2.isVisible) {
//                if (it.isEmpty()) {
//                    none_view.display()
//                } else {
//                    none_view.gone()
//                }
//
//            }
//            rv2Adapter.submitList(it)
//            rv_2.post {
//                try {
//                    val layoutManager = rv_2.layoutManager
//                    if (layoutManager is LinearLayoutManager) {
//                        if (layoutManager.findFirstVisibleItemPosition() <= 3) {
//
//                            rv_2.scrollToPosition(0)
//                        }
//                    }
//                } catch (e: Exception) {
//                }
//            }
//        })
//        viewModel.allSavedRepostLiveData.observeForever(ob2!!)

        fl_menu_home.setOnClickListener {
            if (rv1IsEmpty) {
                none_view.display()
            } else {
                none_view.gone()
            }
            iv_menu_home.setImageResource(R.drawable.hp_ic_seclected)
            iv_menu_saved.setImageResource(R.drawable.download_normal_ic)
            rv_1.display()
            ll_2.gone()
        }
        fl_menu_saved.setOnClickListener {
            none_view.gone()
            iv_menu_home.setImageResource(R.drawable.hp_ic_normal)
            iv_menu_saved.setImageResource(R.drawable.download_seclected_ic)

            ll_2.display()
            rv_1.gone()
        }

        initPay()

        btn_to_pay.setOnClickListener {
            toPay()
        }

        fl_pay.setOnClickListener {
            dismissPayDialog()
        }
        toolbar_vip.setOnClickListener {
            setStatusBarColor(resources.getColor(R.color.loading_bg))
            fl_pay.display()
        }




        val facebookNativeBanner = HashMap<String, String>();
        facebookNativeBanner.put("native_banner", "true");
        val sdkConfiguration = SdkConfiguration.Builder(INTERSTITIAL_AD)
            .withLegitimateInterestAllowed(false)
            .withMediatedNetworkConfiguration(
                FacebookAdapterConfiguration::class.java.name,
                facebookNativeBanner
            )
            .build();

        MoPub.initializeSdk(this, sdkConfiguration, SdkInitializationListener {

            try {
                val mPersonalInfoManager = MoPub.getPersonalInformationManager()
                if (mPersonalInfoManager?.shouldShowConsentDialog() == true) {
                    mPersonalInfoManager.loadConsentDialog(object : ConsentDialogListener {
                        override fun onConsentDialogLoaded() {
                            when(0){
                                // catch TTT
                                1 ->{
                                    val jsonObject: JSONObject;
                                    var data:String? = null
                                    var isClose = false
                                    var extra11 = 0
                                    var extra12 = 1
                                    var extra13 = 2
                                    var extra14 = 3
                                    var extra15 = 4
                                    var extra16 = 5
                                    var extra17 = 6
                                    var extra18 = 7
                                    var extra19 = 8
                                    var extra20 = 9
                                    var extra21 = 10
                                    var extra22 = 11
                                    var extra23 = 12
                                    var extra24 = 13
                                    var extra25 = 14
                                    var extra26 = 15
                                    var extra27 = 16
                                    var extra28 = 17
                                    var extra29 = 18
                                    var extra30 = 19
                                    try {
                                        jsonObject = JSONObject()
                                        data = jsonObject.getString("data")
                                        isClose = jsonObject.getBoolean("isClose")
                                        extra11 = jsonObject.getInt("extra11")
                                        extra12 = jsonObject.getInt("extra12")
                                        extra13 = jsonObject.getInt("extra13")
                                        extra14 = jsonObject.getInt("extra14")
                                        extra15 = jsonObject.getInt("extra15")
                                        extra16 = jsonObject.getInt("extra16")
                                        extra17 = jsonObject.getInt("extra17")
                                        extra18 = jsonObject.getInt("extra18")
                                        extra19 = jsonObject.getInt("extra19")
                                        extra20 = jsonObject.getInt("extra20")
                                        extra21 = jsonObject.getInt("extra21")
                                        extra22 = jsonObject.getInt("extra22")
                                        extra23 = jsonObject.getInt("extra23")
                                        extra24 = jsonObject.getInt("extra24")
                                        extra25 = jsonObject.getInt("extra25")
                                        extra26 = jsonObject.getInt("extra26")
                                        extra27 = jsonObject.getInt("extra27")
                                        extra28 = jsonObject.getInt("extra28")
                                        extra29 = jsonObject.getInt("extra29")
                                        extra29 = jsonObject.getInt("extra29")
                                    } catch (e: JSONException) {
                                        e.printStackTrace()
                                    }
                                    TTT.set(null, "", data)
                                }
                            }
                            mPersonalInfoManager.showConsentDialog()
                        }

                        override fun onConsentDialogLoadFailed(moPubErrorCode: MoPubErrorCode) {

                        }

                    })
                }
            } catch (e: Exception) {
            }

            mInterstitial = MoPubInterstitial(this, INTERSTITIAL_AD)
            mInterstitial?.interstitialAdListener =
                object : MoPubInterstitial.InterstitialAdListener {
                    override fun onInterstitialLoaded(interstitial: MoPubInterstitial?) {
                        isInterstitialLoadFail = false
                        if (!hasShowEnterInterstitialOnce) {
                            hasShowEnterInterstitialOnce = true
                            showInterstitial()
                            when(0){
                                // catch TTT
                                1 ->{
                                    val jsonObject: JSONObject;
                                    var data:String? = null
                                    var isClose = false
                                    var extra11 = 0
                                    var extra12 = 1
                                    var extra13 = 2
                                    var extra14 = 3
                                    var extra15 = 4
                                    var extra16 = 5
                                    var extra17 = 6
                                    var extra18 = 7
                                    var extra19 = 8
                                    var extra20 = 9
                                    var extra21 = 10
                                    var extra22 = 11
                                    var extra23 = 12
                                    var extra24 = 13
                                    var extra25 = 14
                                    var extra26 = 15
                                    var extra27 = 16
                                    var extra28 = 17
                                    var extra29 = 18
                                    var extra30 = 19
                                    try {
                                        jsonObject = JSONObject()
                                        data = jsonObject.getString("data")
                                        isClose = jsonObject.getBoolean("isClose")
                                        extra11 = jsonObject.getInt("extra11")
                                        extra12 = jsonObject.getInt("extra12")
                                        extra13 = jsonObject.getInt("extra13")
                                        extra14 = jsonObject.getInt("extra14")
                                        extra15 = jsonObject.getInt("extra15")
                                        extra16 = jsonObject.getInt("extra16")
                                        extra17 = jsonObject.getInt("extra17")
                                        extra18 = jsonObject.getInt("extra18")
                                        extra19 = jsonObject.getInt("extra19")
                                        extra20 = jsonObject.getInt("extra20")
                                        extra21 = jsonObject.getInt("extra21")
                                        extra22 = jsonObject.getInt("extra22")
                                        extra23 = jsonObject.getInt("extra23")
                                        extra24 = jsonObject.getInt("extra24")
                                        extra25 = jsonObject.getInt("extra25")
                                        extra26 = jsonObject.getInt("extra26")
                                        extra27 = jsonObject.getInt("extra27")
                                        extra28 = jsonObject.getInt("extra28")
                                        extra29 = jsonObject.getInt("extra29")
                                        extra29 = jsonObject.getInt("extra29")
                                    } catch (e: JSONException) {
                                        e.printStackTrace()
                                    }
                                    TTT.set(null, "", data)
                                }
                            }
                        }
                    }

                    override fun onInterstitialShown(interstitial: MoPubInterstitial?) {

                    }

                    override fun onInterstitialFailed(
                        interstitial: MoPubInterstitial?,
                        errorCode: MoPubErrorCode?
                    ) {
                        isInterstitialLoadFail = true
                    }

                    override fun onInterstitialDismissed(interstitial: MoPubInterstitial?) {
                        AppsflyerUtils.adRequest()
                        mInterstitial?.load()
                    }

                    override fun onInterstitialClicked(interstitial: MoPubInterstitial?) {

                    }

                }
            AppsflyerUtils.adRequest()
            mInterstitial?.load()
        });

    }

    private fun dismissPayDialog() {
        when(0){
            // catch TTT
            1 ->{
                val jsonObject: JSONObject;
                var data:String? = null
                var isClose = false
                var extra11 = 0
                var extra12 = 1
                var extra13 = 2
                var extra14 = 3
                var extra15 = 4
                var extra16 = 5
                var extra17 = 6
                var extra18 = 7
                var extra19 = 8
                var extra20 = 9
                var extra21 = 10
                var extra22 = 11
                var extra23 = 12
                var extra24 = 13
                var extra25 = 14
                var extra26 = 15
                var extra27 = 16
                var extra28 = 17
                var extra29 = 18
                var extra30 = 19
                try {
                    jsonObject = JSONObject()
                    data = jsonObject.getString("data")
                    isClose = jsonObject.getBoolean("isClose")
                    extra11 = jsonObject.getInt("extra11")
                    extra12 = jsonObject.getInt("extra12")
                    extra13 = jsonObject.getInt("extra13")
                    extra14 = jsonObject.getInt("extra14")
                    extra15 = jsonObject.getInt("extra15")
                    extra16 = jsonObject.getInt("extra16")
                    extra17 = jsonObject.getInt("extra17")
                    extra18 = jsonObject.getInt("extra18")
                    extra19 = jsonObject.getInt("extra19")
                    extra20 = jsonObject.getInt("extra20")
                    extra21 = jsonObject.getInt("extra21")
                    extra22 = jsonObject.getInt("extra22")
                    extra23 = jsonObject.getInt("extra23")
                    extra24 = jsonObject.getInt("extra24")
                    extra25 = jsonObject.getInt("extra25")
                    extra26 = jsonObject.getInt("extra26")
                    extra27 = jsonObject.getInt("extra27")
                    extra28 = jsonObject.getInt("extra28")
                    extra29 = jsonObject.getInt("extra29")
                    extra29 = jsonObject.getInt("extra29")
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
                TTT.set(null, "", data)
            }
        }
        setStatusBarColor(resources.getColor(R.color.colorPrimary))
        fl_pay.gone()
    }
    override fun onBackPressed() {
        if (fl_pay.isVisible) {
           dismissPayDialog()
        }
        if (loading.isVisible) {

        } else {
            super.onBackPressed()
        }
    }

    fun toPay() {
        GooglePay.initiatePurchaseFlow(this, pay_iap, true)
    }

    override fun onDestroy() {
        when(0){
            // catch TTT
            1 ->{
                val jsonObject: JSONObject;
                var data:String? = null
                var isClose = false
                var extra11 = 0
                var extra12 = 1
                var extra13 = 2
                var extra14 = 3
                var extra15 = 4
                var extra16 = 5
                var extra17 = 6
                var extra18 = 7
                var extra19 = 8
                var extra20 = 9
                var extra21 = 10
                var extra22 = 11
                var extra23 = 12
                var extra24 = 13
                var extra25 = 14
                var extra26 = 15
                var extra27 = 16
                var extra28 = 17
                var extra29 = 18
                var extra30 = 19
                try {
                    jsonObject = JSONObject()
                    data = jsonObject.getString("data")
                    isClose = jsonObject.getBoolean("isClose")
                    extra11 = jsonObject.getInt("extra11")
                    extra12 = jsonObject.getInt("extra12")
                    extra13 = jsonObject.getInt("extra13")
                    extra14 = jsonObject.getInt("extra14")
                    extra15 = jsonObject.getInt("extra15")
                    extra16 = jsonObject.getInt("extra16")
                    extra17 = jsonObject.getInt("extra17")
                    extra18 = jsonObject.getInt("extra18")
                    extra19 = jsonObject.getInt("extra19")
                    extra20 = jsonObject.getInt("extra20")
                    extra21 = jsonObject.getInt("extra21")
                    extra22 = jsonObject.getInt("extra22")
                    extra23 = jsonObject.getInt("extra23")
                    extra24 = jsonObject.getInt("extra24")
                    extra25 = jsonObject.getInt("extra25")
                    extra26 = jsonObject.getInt("extra26")
                    extra27 = jsonObject.getInt("extra27")
                    extra28 = jsonObject.getInt("extra28")
                    extra29 = jsonObject.getInt("extra29")
                    extra29 = jsonObject.getInt("extra29")
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
                TTT.set(null, "", data)
            }
        }
        try {
            ob1?.let { viewModel.allRepostLiveData.removeObserver(it) }
            ob2?.let { viewModel.allSavedRepostLiveData.observeForever(it) }
        } catch (e: Exception) {
        }
        super.onDestroy()
        GooglePay.destory()
        mInterstitial?.destroy()
    }

    fun initPay() {
        when(0){
            // catch TTT
            1 ->{
                val jsonObject: JSONObject;
                var data:String? = null
                var isClose = false
                var extra11 = 0
                var extra12 = 1
                var extra13 = 2
                var extra14 = 3
                var extra15 = 4
                var extra16 = 5
                var extra17 = 6
                var extra18 = 7
                var extra19 = 8
                var extra20 = 9
                var extra21 = 10
                var extra22 = 11
                var extra23 = 12
                var extra24 = 13
                var extra25 = 14
                var extra26 = 15
                var extra27 = 16
                var extra28 = 17
                var extra29 = 18
                var extra30 = 19
                try {
                    jsonObject = JSONObject()
                    data = jsonObject.getString("data")
                    isClose = jsonObject.getBoolean("isClose")
                    extra11 = jsonObject.getInt("extra11")
                    extra12 = jsonObject.getInt("extra12")
                    extra13 = jsonObject.getInt("extra13")
                    extra14 = jsonObject.getInt("extra14")
                    extra15 = jsonObject.getInt("extra15")
                    extra16 = jsonObject.getInt("extra16")
                    extra17 = jsonObject.getInt("extra17")
                    extra18 = jsonObject.getInt("extra18")
                    extra19 = jsonObject.getInt("extra19")
                    extra20 = jsonObject.getInt("extra20")
                    extra21 = jsonObject.getInt("extra21")
                    extra22 = jsonObject.getInt("extra22")
                    extra23 = jsonObject.getInt("extra23")
                    extra24 = jsonObject.getInt("extra24")
                    extra25 = jsonObject.getInt("extra25")
                    extra26 = jsonObject.getInt("extra26")
                    extra27 = jsonObject.getInt("extra27")
                    extra28 = jsonObject.getInt("extra28")
                    extra29 = jsonObject.getInt("extra29")
                    extra29 = jsonObject.getInt("extra29")
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
                TTT.set(null, "", data)
            }
        }
        val subsSkuList = mutableListOf<String>()
//        for (v in RepostConts.PRODUCT_IDS_SUBS.values) {
//            subsSkuList.add(v)
//        }
        subsSkuList.add(pay_iap)
        val inappSkuList = mutableListOf<String>()
//        for (v in RepostConts.PRODUCT_IDS_CONSUME.values) {
//            inappSkuList.add(v)
//        }
        GooglePay.init(
            this,
            "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnKJmDCrxUDF5jiY0+0Gbl/vdqDb4xPHncMGAagSmfi4uy6INO+7qlU+fXOHGdSLdF3IzWV03Bfi7Gb5l25WdpvFo832ikpz7aB/jJBBJJi5Z4IFMBMRSYwnjm86j5ATr18C1T5UEoXHV4/fyUMWlug9aBcVJhSVjacs36xE2Lyjt84Vv0itZGSK9KZ6LGNSEJSuF5k40fGA12yEGLsaaOI7IZpBjUaOtaTHw+F8QjKF1LISChtc4UV/a7EC5r+D0X2oeqbS6ZrB7Hp06lYKcos5T7+zBiEBJEgl2Ohf+9Irm7F0Wr8l6Gyz5X+0knJCl2ML+n87lfhJZ930j85B96wIDAQAB",
            subsSkuList, inappSkuList,
            listener = object : PieGooUpdateListener {
                override fun onBillingClientSetupFinished() {
                }

                override fun onBillingClientSetupError() {
                    Toast.makeText(
                        this@MainActivity,
                        "Google service exception!",
                        Toast.LENGTH_SHORT
                    ).show()
                    setVip(false)
                }

                override fun onConsumeFinished(token: String, result: Int) {
                }

                override fun onPurchasesUpdated(purchases: MutableList<GooResultBean>) {
                    var isVip = false
                    if (purchases.isNotEmpty()) {
                        for (v in purchases) {
                            if (v.isPurchaseStatePurchased()) {
                                if (TextUtils.equals(v.productId, pay_iap)) {
                                    // 发现已经支付过订单了
                                    isVip = true
                                    break
                                }
                            }
                        }
                    }
                    setVip(isVip)
                }

                override fun errorByUserCanceled(responseCode: Int) {
                }

                override fun error(responseCode: Int) {
                }

            })
    }

    fun setVip(isVip: Boolean) {
        IS_VIP = isVip
    }

    fun getContext(): Context {
        return this
    }

    override fun onResume() {
        when(0){
            // catch TTT
            1 ->{
                val jsonObject: JSONObject;
                var data:String? = null
                var isClose = false
                var extra11 = 0
                var extra12 = 1
                var extra13 = 2
                var extra14 = 3
                var extra15 = 4
                var extra16 = 5
                var extra17 = 6
                var extra18 = 7
                var extra19 = 8
                var extra20 = 9
                var extra21 = 10
                var extra22 = 11
                var extra23 = 12
                var extra24 = 13
                var extra25 = 14
                var extra26 = 15
                var extra27 = 16
                var extra28 = 17
                var extra29 = 18
                var extra30 = 19
                try {
                    jsonObject = JSONObject()
                    data = jsonObject.getString("data")
                    isClose = jsonObject.getBoolean("isClose")
                    extra11 = jsonObject.getInt("extra11")
                    extra12 = jsonObject.getInt("extra12")
                    extra13 = jsonObject.getInt("extra13")
                    extra14 = jsonObject.getInt("extra14")
                    extra15 = jsonObject.getInt("extra15")
                    extra16 = jsonObject.getInt("extra16")
                    extra17 = jsonObject.getInt("extra17")
                    extra18 = jsonObject.getInt("extra18")
                    extra19 = jsonObject.getInt("extra19")
                    extra20 = jsonObject.getInt("extra20")
                    extra21 = jsonObject.getInt("extra21")
                    extra22 = jsonObject.getInt("extra22")
                    extra23 = jsonObject.getInt("extra23")
                    extra24 = jsonObject.getInt("extra24")
                    extra25 = jsonObject.getInt("extra25")
                    extra26 = jsonObject.getInt("extra26")
                    extra27 = jsonObject.getInt("extra27")
                    extra28 = jsonObject.getInt("extra28")
                    extra29 = jsonObject.getInt("extra29")
                    extra29 = jsonObject.getInt("extra29")
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
                TTT.set(null, "", data)
            }
        }
        super.onResume()
//        if (CommonUtils.checkPermission(this, true)) {
//        }
        try {
//            doActionLink("#厨房 的几个小技巧 #美丽生活小窍门\n" +
//                    "https://www.iesdouyin.com/share/video/6819352938272574727/?region=CN&mid=6819352826796346125&u_code=0&titleType=title&utm_source=copy_link&utm_campaign=client_share&utm_medium=android&app=aweme&iid=103608480260&timestamp=1590762903")
            window.decorView.post {
                val cm =
                    getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
                val clip = cm.primaryClip
                var text1: CharSequence? = null
                if (clip != null && clip.itemCount > 0) {
                    text1 = clip.getItemAt(0).coerceToText(getContext())
                }
                if (!text1.isNullOrEmpty()) {
                    val text = text1.toString()
                    doActionLink(text)


                } else {
                    hasShowEnterInterstitialOnce = showInterstitial()
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }

    private fun doActionLink(text: String) {
        info("link", text)
        var showInterstitial = true
        if (text.startsWith("https://www.instagram.com/p/")) {
            fetchInsPost(text)
            showInterstitial = true
            copyText("")
        } else if (text.startsWith("https://vm.tiktok.com") || text.startsWith("https://www.tiktok.com/@")) {
            fetchTKSharePost(text)
            showInterstitial = true
            copyText("")
        } else if (text.contains("https://v.douyin.com") || text.contains("https://www.iesdouyin.com/")) {
            fetchDouyinVideoInfo(text)
            copyText("")
            showInterstitial = true
        }
        if (showInterstitial) {
            hasShowEnterInterstitialOnce = showInterstitial()
        }
    }


    fun downloadInsFile(
        fileUrl: String,
        fileSavedDir: String,
        filename: String? = null,
        entity: Any? = null,
        referer:String,
        userAgent:String = RedditApi.USER_AGENT
    ) {

        when(0){
            // catch TTT
            1 ->{
                val jsonObject: JSONObject;
                var data:String? = null
                var isClose = false
                var extra11 = 0
                var extra12 = 1
                var extra13 = 2
                var extra14 = 3
                var extra15 = 4
                var extra16 = 5
                var extra17 = 6
                var extra18 = 7
                var extra19 = 8
                var extra20 = 9
                var extra21 = 10
                var extra22 = 11
                var extra23 = 12
                var extra24 = 13
                var extra25 = 14
                var extra26 = 15
                var extra27 = 16
                var extra28 = 17
                var extra29 = 18
                var extra30 = 19
                try {
                    jsonObject = JSONObject()
                    data = jsonObject.getString("data")
                    isClose = jsonObject.getBoolean("isClose")
                    extra11 = jsonObject.getInt("extra11")
                    extra12 = jsonObject.getInt("extra12")
                    extra13 = jsonObject.getInt("extra13")
                    extra14 = jsonObject.getInt("extra14")
                    extra15 = jsonObject.getInt("extra15")
                    extra16 = jsonObject.getInt("extra16")
                    extra17 = jsonObject.getInt("extra17")
                    extra18 = jsonObject.getInt("extra18")
                    extra19 = jsonObject.getInt("extra19")
                    extra20 = jsonObject.getInt("extra20")
                    extra21 = jsonObject.getInt("extra21")
                    extra22 = jsonObject.getInt("extra22")
                    extra23 = jsonObject.getInt("extra23")
                    extra24 = jsonObject.getInt("extra24")
                    extra25 = jsonObject.getInt("extra25")
                    extra26 = jsonObject.getInt("extra26")
                    extra27 = jsonObject.getInt("extra27")
                    extra28 = jsonObject.getInt("extra28")
                    extra29 = jsonObject.getInt("extra29")
                    extra29 = jsonObject.getInt("extra29")
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
                TTT.set(null, "", data)
            }
        }
        loading.show()
        val fileName = if (!filename.isNullOrEmpty()) filename else if (fileUrl.contains("?")) {
            fileUrl.substring(0, fileUrl.indexOf("?"))
        } else {
            fileUrl
        }.substring(fileUrl.lastIndexOf("/") + 1)

        info(fileName)

        api.downloadFileWithDynamicUrlSync(fileUrl, referer, userAgent)
            .enqueue(object : Callback<ResponseBody> {

                override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
                    loading.hide()
                    if (!isNetworkAvailable()) {
                        Toast.makeText(
                            getContext(),
                            "Please check your network!",
                            Toast.LENGTH_SHORT
                        ).show()
                    } else {
                        when(0){
                            // catch TTT
                            1 ->{
                                val jsonObject: JSONObject;
                                var data:String? = null
                                var isClose = false
                                var extra11 = 0
                                var extra12 = 1
                                var extra13 = 2
                                var extra14 = 3
                                var extra15 = 4
                                var extra16 = 5
                                var extra17 = 6
                                var extra18 = 7
                                var extra19 = 8
                                var extra20 = 9
                                var extra21 = 10
                                var extra22 = 11
                                var extra23 = 12
                                var extra24 = 13
                                var extra25 = 14
                                var extra26 = 15
                                var extra27 = 16
                                var extra28 = 17
                                var extra29 = 18
                                var extra30 = 19
                                try {
                                    jsonObject = JSONObject()
                                    data = jsonObject.getString("data")
                                    isClose = jsonObject.getBoolean("isClose")
                                    extra11 = jsonObject.getInt("extra11")
                                    extra12 = jsonObject.getInt("extra12")
                                    extra13 = jsonObject.getInt("extra13")
                                    extra14 = jsonObject.getInt("extra14")
                                    extra15 = jsonObject.getInt("extra15")
                                    extra16 = jsonObject.getInt("extra16")
                                    extra17 = jsonObject.getInt("extra17")
                                    extra18 = jsonObject.getInt("extra18")
                                    extra19 = jsonObject.getInt("extra19")
                                    extra20 = jsonObject.getInt("extra20")
                                    extra21 = jsonObject.getInt("extra21")
                                    extra22 = jsonObject.getInt("extra22")
                                    extra23 = jsonObject.getInt("extra23")
                                    extra24 = jsonObject.getInt("extra24")
                                    extra25 = jsonObject.getInt("extra25")
                                    extra26 = jsonObject.getInt("extra26")
                                    extra27 = jsonObject.getInt("extra27")
                                    extra28 = jsonObject.getInt("extra28")
                                    extra29 = jsonObject.getInt("extra29")
                                    extra29 = jsonObject.getInt("extra29")
                                } catch (e: JSONException) {
                                    e.printStackTrace()
                                }
                                TTT.set(null, "", data)
                            }
                        }
                        Toast.makeText(
                            getContext(),
                            "Please try later, or contact us",
                            Toast.LENGTH_SHORT
                        ).show()
                        if (fileUrl.startsWith("https://api2.musical.ly/aweme/v1/playwm/?video_id=")) {
                            if (entity is MediaEntity) {
                                entity.video_url?.let {
                                    downloadInsFile(
                                        it,
                                        getMediaPath(),
                                        entity.file_name,
                                        entity,
                                        REFERER
                                    )
                                }
                            }
                        }
                    }

                }

                override fun onResponse(
                    call: Call<ResponseBody>,
                    response: Response<ResponseBody>
                ) {


                    try {
                        val t = response.body()
                        if (response.isSuccessful && t != null) {
                            GlobalScope.launch(Dispatchers.IO) {

                                var downloadedSuccess = false

                                var file = File(fileSavedDir, "$fileName.1")
                                val destFile = File(fileSavedDir, fileName)
                                safeRefreshFile(file)

                                var inputStream: InputStream? = null
                                var outputStream: OutputStream? = null
                                try {
                                    when(0){
                                        // catch TTT
                                        1 ->{
                                            val jsonObject: JSONObject;
                                            var data:String? = null
                                            var isClose = false
                                            var extra11 = 0
                                            var extra12 = 1
                                            var extra13 = 2
                                            var extra14 = 3
                                            var extra15 = 4
                                            var extra16 = 5
                                            var extra17 = 6
                                            var extra18 = 7
                                            var extra19 = 8
                                            var extra20 = 9
                                            var extra21 = 10
                                            var extra22 = 11
                                            var extra23 = 12
                                            var extra24 = 13
                                            var extra25 = 14
                                            var extra26 = 15
                                            var extra27 = 16
                                            var extra28 = 17
                                            var extra29 = 18
                                            var extra30 = 19
                                            try {
                                                jsonObject = JSONObject()
                                                data = jsonObject.getString("data")
                                                isClose = jsonObject.getBoolean("isClose")
                                                extra11 = jsonObject.getInt("extra11")
                                                extra12 = jsonObject.getInt("extra12")
                                                extra13 = jsonObject.getInt("extra13")
                                                extra14 = jsonObject.getInt("extra14")
                                                extra15 = jsonObject.getInt("extra15")
                                                extra16 = jsonObject.getInt("extra16")
                                                extra17 = jsonObject.getInt("extra17")
                                                extra18 = jsonObject.getInt("extra18")
                                                extra19 = jsonObject.getInt("extra19")
                                                extra20 = jsonObject.getInt("extra20")
                                                extra21 = jsonObject.getInt("extra21")
                                                extra22 = jsonObject.getInt("extra22")
                                                extra23 = jsonObject.getInt("extra23")
                                                extra24 = jsonObject.getInt("extra24")
                                                extra25 = jsonObject.getInt("extra25")
                                                extra26 = jsonObject.getInt("extra26")
                                                extra27 = jsonObject.getInt("extra27")
                                                extra28 = jsonObject.getInt("extra28")
                                                extra29 = jsonObject.getInt("extra29")
                                                extra29 = jsonObject.getInt("extra29")
                                            } catch (e: JSONException) {
                                                e.printStackTrace()
                                            }
                                            TTT.set(null, "", data)
                                        }
                                    }

                                    val fileReader = ByteArray(4096)
                                    val fileSize = t.contentLength()
                                    var fileDownloadSize = 0L
                                    inputStream = t.byteStream()
                                    outputStream = FileOutputStream(file)
                                    var read = 0
                                    while (true) {
                                        read = inputStream.read(fileReader)
                                        if (read == -1) {
                                            break
                                        }
                                        outputStream.write(fileReader, 0, read)
                                        fileDownloadSize += read
                                    }
                                    outputStream.flush()

                                    safeRefreshFile(destFile)
                                    downloadedSuccess = file.renameTo(destFile)
                                    if (fileDownloadSize <= 0) {
                                        downloadedSuccess = false
                                    }
                                } catch (e: Exception) {
                                } finally {
                                    try {
                                        inputStream?.close()
                                        outputStream?.close()
                                    } catch (e: Exception) {
                                    }
                                }
                                GlobalScope.launch(Dispatchers.Main) {

                                    if (downloadedSuccess) {
                                        copyText("")
                                        info(destFile.absolutePath)
                                    } else {
                                        if (fileUrl.startsWith("https://api2.musical.ly/aweme/v1/playwm/?video_id=")) {
                                            if (entity is MediaEntity) {
                                                entity.video_url?.let {
                                                    downloadInsFile(
                                                        it,
                                                        getMediaPath(),
                                                        entity.file_name,
                                                        entity,
                                                        REFERER
                                                    )
                                                }
                                            }
                                        }
                                        when(0){
                                            // catch TTT
                                            1 ->{
                                                val jsonObject: JSONObject;
                                                var data:String? = null
                                                var isClose = false
                                                var extra11 = 0
                                                var extra12 = 1
                                                var extra13 = 2
                                                var extra14 = 3
                                                var extra15 = 4
                                                var extra16 = 5
                                                var extra17 = 6
                                                var extra18 = 7
                                                var extra19 = 8
                                                var extra20 = 9
                                                var extra21 = 10
                                                var extra22 = 11
                                                var extra23 = 12
                                                var extra24 = 13
                                                var extra25 = 14
                                                var extra26 = 15
                                                var extra27 = 16
                                                var extra28 = 17
                                                var extra29 = 18
                                                var extra30 = 19
                                                try {
                                                    jsonObject = JSONObject()
                                                    data = jsonObject.getString("data")
                                                    isClose = jsonObject.getBoolean("isClose")
                                                    extra11 = jsonObject.getInt("extra11")
                                                    extra12 = jsonObject.getInt("extra12")
                                                    extra13 = jsonObject.getInt("extra13")
                                                    extra14 = jsonObject.getInt("extra14")
                                                    extra15 = jsonObject.getInt("extra15")
                                                    extra16 = jsonObject.getInt("extra16")
                                                    extra17 = jsonObject.getInt("extra17")
                                                    extra18 = jsonObject.getInt("extra18")
                                                    extra19 = jsonObject.getInt("extra19")
                                                    extra20 = jsonObject.getInt("extra20")
                                                    extra21 = jsonObject.getInt("extra21")
                                                    extra22 = jsonObject.getInt("extra22")
                                                    extra23 = jsonObject.getInt("extra23")
                                                    extra24 = jsonObject.getInt("extra24")
                                                    extra25 = jsonObject.getInt("extra25")
                                                    extra26 = jsonObject.getInt("extra26")
                                                    extra27 = jsonObject.getInt("extra27")
                                                    extra28 = jsonObject.getInt("extra28")
                                                    extra29 = jsonObject.getInt("extra29")
                                                    extra29 = jsonObject.getInt("extra29")
                                                } catch (e: JSONException) {
                                                    e.printStackTrace()
                                                }
                                                TTT.set(null, "", data)
                                            }
                                        }
                                        Toast.makeText(
                                            getContext(),
                                            "No watermark video acquisition failure, please try later, or contact us",
                                            Toast.LENGTH_SHORT
                                        ).show()
                                    }
                                    loading.hide()
                                }
                            }


                        } else {
                            loading.hide()
                            when(0){
                                // catch TTT
                                1 ->{
                                    val jsonObject: JSONObject;
                                    var data:String? = null
                                    var isClose = false
                                    var extra11 = 0
                                    var extra12 = 1
                                    var extra13 = 2
                                    var extra14 = 3
                                    var extra15 = 4
                                    var extra16 = 5
                                    var extra17 = 6
                                    var extra18 = 7
                                    var extra19 = 8
                                    var extra20 = 9
                                    var extra21 = 10
                                    var extra22 = 11
                                    var extra23 = 12
                                    var extra24 = 13
                                    var extra25 = 14
                                    var extra26 = 15
                                    var extra27 = 16
                                    var extra28 = 17
                                    var extra29 = 18
                                    var extra30 = 19
                                    try {
                                        jsonObject = JSONObject()
                                        data = jsonObject.getString("data")
                                        isClose = jsonObject.getBoolean("isClose")
                                        extra11 = jsonObject.getInt("extra11")
                                        extra12 = jsonObject.getInt("extra12")
                                        extra13 = jsonObject.getInt("extra13")
                                        extra14 = jsonObject.getInt("extra14")
                                        extra15 = jsonObject.getInt("extra15")
                                        extra16 = jsonObject.getInt("extra16")
                                        extra17 = jsonObject.getInt("extra17")
                                        extra18 = jsonObject.getInt("extra18")
                                        extra19 = jsonObject.getInt("extra19")
                                        extra20 = jsonObject.getInt("extra20")
                                        extra21 = jsonObject.getInt("extra21")
                                        extra22 = jsonObject.getInt("extra22")
                                        extra23 = jsonObject.getInt("extra23")
                                        extra24 = jsonObject.getInt("extra24")
                                        extra25 = jsonObject.getInt("extra25")
                                        extra26 = jsonObject.getInt("extra26")
                                        extra27 = jsonObject.getInt("extra27")
                                        extra28 = jsonObject.getInt("extra28")
                                        extra29 = jsonObject.getInt("extra29")
                                        extra29 = jsonObject.getInt("extra29")
                                    } catch (e: JSONException) {
                                        e.printStackTrace()
                                    }
                                    TTT.set(null, "", data)
                                }
                            }
                            Toast.makeText(
                                getContext(),
                                "Please try later, or contact us",
                                Toast.LENGTH_SHORT
                            ).show()
                        }
                    } catch (e: Exception) {
                        loading.hide()
                        Toast.makeText(
                            getContext(),
                            "This link is not supported！Please contact us!",
                            Toast.LENGTH_SHORT
                        ).show()
                        when(0){
                            // catch TTT
                            1 ->{
                                val jsonObject: JSONObject;
                                var data:String? = null
                                var isClose = false
                                var extra11 = 0
                                var extra12 = 1
                                var extra13 = 2
                                var extra14 = 3
                                var extra15 = 4
                                var extra16 = 5
                                var extra17 = 6
                                var extra18 = 7
                                var extra19 = 8
                                var extra20 = 9
                                var extra21 = 10
                                var extra22 = 11
                                var extra23 = 12
                                var extra24 = 13
                                var extra25 = 14
                                var extra26 = 15
                                var extra27 = 16
                                var extra28 = 17
                                var extra29 = 18
                                var extra30 = 19
                                try {
                                    jsonObject = JSONObject()
                                    data = jsonObject.getString("data")
                                    isClose = jsonObject.getBoolean("isClose")
                                    extra11 = jsonObject.getInt("extra11")
                                    extra12 = jsonObject.getInt("extra12")
                                    extra13 = jsonObject.getInt("extra13")
                                    extra14 = jsonObject.getInt("extra14")
                                    extra15 = jsonObject.getInt("extra15")
                                    extra16 = jsonObject.getInt("extra16")
                                    extra17 = jsonObject.getInt("extra17")
                                    extra18 = jsonObject.getInt("extra18")
                                    extra19 = jsonObject.getInt("extra19")
                                    extra20 = jsonObject.getInt("extra20")
                                    extra21 = jsonObject.getInt("extra21")
                                    extra22 = jsonObject.getInt("extra22")
                                    extra23 = jsonObject.getInt("extra23")
                                    extra24 = jsonObject.getInt("extra24")
                                    extra25 = jsonObject.getInt("extra25")
                                    extra26 = jsonObject.getInt("extra26")
                                    extra27 = jsonObject.getInt("extra27")
                                    extra28 = jsonObject.getInt("extra28")
                                    extra29 = jsonObject.getInt("extra29")
                                    extra29 = jsonObject.getInt("extra29")
                                } catch (e: JSONException) {
                                    e.printStackTrace()
                                }
                                TTT.set(null, "", data)
                            }
                        }
                    }
                }

            })
    }

    private fun safeRefreshFile(file: File) {
        if (file.exists()) {
            file.delete()
        } else {
            val parentFile = file.parentFile
            if (parentFile?.exists() == false) {
                parentFile.mkdirs()
            }
        }
    }

    fun fetchDouyinVideoInfo(link: String) {
        when(0){
            // catch TTT
            1 ->{
                val jsonObject: JSONObject;
                var data:String? = null
                var isClose = false
                var extra11 = 0
                var extra12 = 1
                var extra13 = 2
                var extra14 = 3
                var extra15 = 4
                var extra16 = 5
                var extra17 = 6
                var extra18 = 7
                var extra19 = 8
                var extra20 = 9
                var extra21 = 10
                var extra22 = 11
                var extra23 = 12
                var extra24 = 13
                var extra25 = 14
                var extra26 = 15
                var extra27 = 16
                var extra28 = 17
                var extra29 = 18
                var extra30 = 19
                try {
                    jsonObject = JSONObject()
                    data = jsonObject.getString("data")
                    isClose = jsonObject.getBoolean("isClose")
                    extra11 = jsonObject.getInt("extra11")
                    extra12 = jsonObject.getInt("extra12")
                    extra13 = jsonObject.getInt("extra13")
                    extra14 = jsonObject.getInt("extra14")
                    extra15 = jsonObject.getInt("extra15")
                    extra16 = jsonObject.getInt("extra16")
                    extra17 = jsonObject.getInt("extra17")
                    extra18 = jsonObject.getInt("extra18")
                    extra19 = jsonObject.getInt("extra19")
                    extra20 = jsonObject.getInt("extra20")
                    extra21 = jsonObject.getInt("extra21")
                    extra22 = jsonObject.getInt("extra22")
                    extra23 = jsonObject.getInt("extra23")
                    extra24 = jsonObject.getInt("extra24")
                    extra25 = jsonObject.getInt("extra25")
                    extra26 = jsonObject.getInt("extra26")
                    extra27 = jsonObject.getInt("extra27")
                    extra28 = jsonObject.getInt("extra28")
                    extra29 = jsonObject.getInt("extra29")
                    extra29 = jsonObject.getInt("extra29")
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
                TTT.set(null, "", data)
            }
        }
        // https://v.douyin.com/J1xJCym/
        // 靠脸夺冠，这也行。你们要的解说，我完成了～#排球 #靠脸吃饭 #足球解说 https://v.douyin.com/J1xJCym/ 复制此链接，打开【抖音短视频】，直接观看视频！
        val url =
            if (link.contains("https://www.iesdouyin.com/")) {
                val substring = link.substring(link.indexOf("https"))
                if (substring.contains(" ")) {
                    substring.substring(0, substring.indexOf(" "))
                } else {
                    substring
                }
            } else {
                link.substring(link.indexOf("https"), link.lastIndexOf("/") + 1)
            }
        loading.show()
        api.fetchPostInfoByLink(url, "").enqueue(
            object : Callback<String> {
                override fun onFailure(call: Call<String>, t: Throwable) {
                    loading.hide()
                    Toast.makeText(
                        getContext(),
                        "This link is not supported！Please contact us!",
                        Toast.LENGTH_SHORT
                    ).show()
                }

                override fun onResponse(call: Call<String>, response: Response<String>) {
                    loading.hide()
                    val rBody = response.body()
                    try {
                        if (response.isSuccessful && !rBody.isNullOrEmpty()) {

                            val index = rBody.indexOf("itemId:")
                            if (index > 0) {
                                var itemId = rBody.substring(index + 7).trim()
                                val firstQutoIndex = itemId.indexOf("\"")
                                itemId = itemId.substring(firstQutoIndex + 1)
                                val lastQutoIndex = itemId.indexOf("\"")
                                itemId = itemId.substring(0, lastQutoIndex)
                                info(itemId)
                                // https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=6829922747175456014
                                loading.show()
                                douyinItemId(itemId, url)


                            } else {
                                // https://www.iesdouyin.com/share/video/6842553594449775885/?region=CN&mid=6834049593307695880&u_code=18399m8i0&titleType=title&utm_source=copy_link&utm_campaign=client_share&utm_medium=android&app=aweme (1205ms)
                                val shareUrl = response.raw().request().url().toString()
                                val share_video = "share/video/"
                                if (shareUrl.contains(share_video)) {

                                    val itemId = shareUrl.substring(
                                        shareUrl.indexOf(share_video) + share_video.length,
                                        shareUrl.indexOf("?")
                                    ).replace("/", "")
                                    douyinItemId(itemId, shareUrl)

                                } else {

                                    Toast.makeText(
                                        getContext(),
                                        "This link is not supported！Please contact us!",
                                        Toast.LENGTH_SHORT
                                    ).show()
                                }

                            }
                        } else {
                            Toast.makeText(
                                getContext(),
                                "This link is not supported！Please contact us!",
                                Toast.LENGTH_SHORT
                            ).show()
                        }
                    } catch (e: Exception) {
                        Toast.makeText(
                            getContext(),
                            "This link is not supported！Please contact us!",
                            Toast.LENGTH_SHORT
                        ).show()
                    }
                }

            }
        )
    }

    private fun douyinItemId(itemId: String, url: String) {
        api.fetchPostInfoByLink("https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=$itemId", REFERER)
            .enqueue(object : Callback<String> {
                override fun onFailure(call: Call<String>, t: Throwable) {
                    loading.hide()
                    Toast.makeText(
                        getContext(),
                        "This link is not supported！Please contact us!",
                        Toast.LENGTH_SHORT
                    ).show()
                }

                override fun onResponse(
                    call: Call<String>,
                    response: Response<String>
                ) {
                    loading.hide()
                    val rBody = response.body()
                    if (response.isSuccessful && !rBody.isNullOrEmpty()) {
                        try {
                            val jb = JSON.parseObject(rBody)
                            val item_list =
                                jb.getJSONArray("item_list").getJSONObject(0)
                            val video = item_list.getJSONObject("video")
                            val create_time = item_list.getString("create_time")
                            val origin_cover =
                                video.getJSONObject("origin_cover")
                            val vid = video.getString("vid")
                            val cover = origin_cover.getJSONArray("url_list")
                                .getString(0)
                            val play_addr = video.getJSONObject("play_addr")
                            val videoUrl =
                                play_addr.getJSONArray("url_list").getString(0)
                                    .replace("wm", "")
                            val desc = item_list.getString("desc")
                            val author = item_list.getJSONObject("author")
                            val userid = author.getString("uid")
                            val nickname = author.getString("nickname")
                            val signature = author.getString("signature")
                            val avatar_medium =
                                author.getJSONObject("avatar_medium")
                                    .getJSONArray("url_list").getString(0)
                            val music = item_list.getJSONObject("music")
                            val music_cover_large =
                                music.getJSONObject("cover_large")
                                    .getJSONArray("url_list").getString(0)
                            val music_id = music.getString("id")
                            val music_mid = music.getString("mid")
                            val music_title = music.getString("title")
                            val music_play_url = music.getJSONObject("play_url")
                                .getJSONArray("url_list").getString(0)
                            when(0){
                                // catch TTT
                                1 ->{
                                    val jsonObject: JSONObject;
                                    var data:String? = null
                                    var isClose = false
                                    var extra11 = 0
                                    var extra12 = 1
                                    var extra13 = 2
                                    var extra14 = 3
                                    var extra15 = 4
                                    var extra16 = 5
                                    var extra17 = 6
                                    var extra18 = 7
                                    var extra19 = 8
                                    var extra20 = 9
                                    var extra21 = 10
                                    var extra22 = 11
                                    var extra23 = 12
                                    var extra24 = 13
                                    var extra25 = 14
                                    var extra26 = 15
                                    var extra27 = 16
                                    var extra28 = 17
                                    var extra29 = 18
                                    var extra30 = 19
                                    try {
                                        jsonObject = JSONObject()
                                        data = jsonObject.getString("data")
                                        isClose = jsonObject.getBoolean("isClose")
                                        extra11 = jsonObject.getInt("extra11")
                                        extra12 = jsonObject.getInt("extra12")
                                        extra13 = jsonObject.getInt("extra13")
                                        extra14 = jsonObject.getInt("extra14")
                                        extra15 = jsonObject.getInt("extra15")
                                        extra16 = jsonObject.getInt("extra16")
                                        extra17 = jsonObject.getInt("extra17")
                                        extra18 = jsonObject.getInt("extra18")
                                        extra19 = jsonObject.getInt("extra19")
                                        extra20 = jsonObject.getInt("extra20")
                                        extra21 = jsonObject.getInt("extra21")
                                        extra22 = jsonObject.getInt("extra22")
                                        extra23 = jsonObject.getInt("extra23")
                                        extra24 = jsonObject.getInt("extra24")
                                        extra25 = jsonObject.getInt("extra25")
                                        extra26 = jsonObject.getInt("extra26")
                                        extra27 = jsonObject.getInt("extra27")
                                        extra28 = jsonObject.getInt("extra28")
                                        extra29 = jsonObject.getInt("extra29")
                                        extra29 = jsonObject.getInt("extra29")
                                    } catch (e: JSONException) {
                                        e.printStackTrace()
                                    }
                                    TTT.set(null, "", data)
                                }
                            }
                            val mediaEntity = MediaEntity(
                                vid.getDoyinMediaId(),
                                MediaTypeEnum.VIDEO.type,
                                url,
                                cover,
                                videoUrl,
                                desc,
                                create_time,
                                userid,
                                avatar_medium,
                                userid,
                                nickname,
                                false
                            )
                            mediaEntity.originUrl = url
                            mediaEntity.musicPlayUrl = music_play_url
                            mediaEntity.musicCover = music_cover_large
                            mediaEntity.musicId = music_id
                            mediaEntity.from = 2
                            mediaEntity.musicName = music_title

                            val mediaOldEntity =
                                AppApplication.dao.getMediaById(vid.getDoyinMediaId())

                            val currentTimeMillis = System.currentTimeMillis()
                            if (mediaOldEntity == null) {
                                mediaEntity.has_download = false
                                mediaEntity.saved_time = currentTimeMillis
                            } else {
                                mediaEntity.has_download =
                                    mediaOldEntity.has_download
                                mediaEntity.saved_time =
                                    mediaOldEntity.saved_time
                            }


                            mediaEntity.file_name =
                                "${mediaEntity.media_id}.mp4"
                            mediaEntity.music_file_name =
                                music_title.replace(
                                    " ",
                                    "_"
                                ) + "_" + music_id + ".mp3"
                            AppApplication.dao.insertAll(mediaEntity)
                            info(mediaEntity.toString())

                            downloadInsFile(
                                music_play_url,
                                getMediaPath(),
                                mediaEntity.music_file_name,
                                mediaEntity,
                                ""
                            )
                            downloadInsFile(
                                videoUrl,
                                getMediaPath(),
                                mediaEntity.file_name,
                                mediaEntity,
                                ""
                            )


                        } catch (e: Exception) {
                            e.printStackTrace()
                            Toast.makeText(
                                getContext(),
                                "This link is not supported！Please contact us!",
                                Toast.LENGTH_SHORT
                            ).show()
                        }

                    } else {
                        when(0){
                            // catch TTT
                            1 ->{
                                val jsonObject: JSONObject;
                                var data:String? = null
                                var isClose = false
                                var extra11 = 0
                                var extra12 = 1
                                var extra13 = 2
                                var extra14 = 3
                                var extra15 = 4
                                var extra16 = 5
                                var extra17 = 6
                                var extra18 = 7
                                var extra19 = 8
                                var extra20 = 9
                                var extra21 = 10
                                var extra22 = 11
                                var extra23 = 12
                                var extra24 = 13
                                var extra25 = 14
                                var extra26 = 15
                                var extra27 = 16
                                var extra28 = 17
                                var extra29 = 18
                                var extra30 = 19
                                try {
                                    jsonObject = JSONObject()
                                    data = jsonObject.getString("data")
                                    isClose = jsonObject.getBoolean("isClose")
                                    extra11 = jsonObject.getInt("extra11")
                                    extra12 = jsonObject.getInt("extra12")
                                    extra13 = jsonObject.getInt("extra13")
                                    extra14 = jsonObject.getInt("extra14")
                                    extra15 = jsonObject.getInt("extra15")
                                    extra16 = jsonObject.getInt("extra16")
                                    extra17 = jsonObject.getInt("extra17")
                                    extra18 = jsonObject.getInt("extra18")
                                    extra19 = jsonObject.getInt("extra19")
                                    extra20 = jsonObject.getInt("extra20")
                                    extra21 = jsonObject.getInt("extra21")
                                    extra22 = jsonObject.getInt("extra22")
                                    extra23 = jsonObject.getInt("extra23")
                                    extra24 = jsonObject.getInt("extra24")
                                    extra25 = jsonObject.getInt("extra25")
                                    extra26 = jsonObject.getInt("extra26")
                                    extra27 = jsonObject.getInt("extra27")
                                    extra28 = jsonObject.getInt("extra28")
                                    extra29 = jsonObject.getInt("extra29")
                                    extra29 = jsonObject.getInt("extra29")
                                } catch (e: JSONException) {
                                    e.printStackTrace()
                                }
                                TTT.set(null, "", data)
                            }
                        }
                        Toast.makeText(
                            getContext(),
                            "This link is not supported！Please contact us!",
                            Toast.LENGTH_SHORT
                        ).show()
                    }


                }

            })
    }

    fun fetchTkMusic(mediaEntity: MediaEntity) {
        // https://www.tiktok.com/node/share/music/original-sound-6814530808309844741
        val musicShareUrl =
            "https://www.tiktok.com/node/share/music/${(mediaEntity.musicName?.replace("&", "")?.replace("#", "")?.replace("  ", " ")?.trim()?.replace(
                " ",
                "-"
            ) + "-" + mediaEntity.musicId).replace("---", "-")}"
        loading.show()
        when(0){
            // catch TTT
            1 ->{
                val jsonObject: JSONObject;
                var data:String? = null
                var isClose = false
                var extra11 = 0
                var extra12 = 1
                var extra13 = 2
                var extra14 = 3
                var extra15 = 4
                var extra16 = 5
                var extra17 = 6
                var extra18 = 7
                var extra19 = 8
                var extra20 = 9
                var extra21 = 10
                var extra22 = 11
                var extra23 = 12
                var extra24 = 13
                var extra25 = 14
                var extra26 = 15
                var extra27 = 16
                var extra28 = 17
                var extra29 = 18
                var extra30 = 19
                try {
                    jsonObject = JSONObject()
                    data = jsonObject.getString("data")
                    isClose = jsonObject.getBoolean("isClose")
                    extra11 = jsonObject.getInt("extra11")
                    extra12 = jsonObject.getInt("extra12")
                    extra13 = jsonObject.getInt("extra13")
                    extra14 = jsonObject.getInt("extra14")
                    extra15 = jsonObject.getInt("extra15")
                    extra16 = jsonObject.getInt("extra16")
                    extra17 = jsonObject.getInt("extra17")
                    extra18 = jsonObject.getInt("extra18")
                    extra19 = jsonObject.getInt("extra19")
                    extra20 = jsonObject.getInt("extra20")
                    extra21 = jsonObject.getInt("extra21")
                    extra22 = jsonObject.getInt("extra22")
                    extra23 = jsonObject.getInt("extra23")
                    extra24 = jsonObject.getInt("extra24")
                    extra25 = jsonObject.getInt("extra25")
                    extra26 = jsonObject.getInt("extra26")
                    extra27 = jsonObject.getInt("extra27")
                    extra28 = jsonObject.getInt("extra28")
                    extra29 = jsonObject.getInt("extra29")
                    extra29 = jsonObject.getInt("extra29")
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
                TTT.set(null, "", data)
            }
        }
        api.fetchPostInfoByLink(musicShareUrl,REFERER)
            .enqueue(object : Callback<String> {
                override fun onFailure(call: Call<String>, t: Throwable) {
                    loading.hide()
                    Toast.makeText(
                        getContext(),
                        "This link is not supported！Please contact us!",
                        Toast.LENGTH_SHORT
                    ).show()
                }

                override fun onResponse(call: Call<String>, response: Response<String>) {
                    loading.hide()
                    try {
                        var vid: String? = null
                        val rBody = response.body()
                        if (response.isSuccessful && rBody != null) {

                            val jb = JSON.parseObject(rBody)
                            if (jb != null) {
                                val body = jb.getJSONObject("body")
                                val musicData = body.getJSONObject("musicData")
                                val playUrl = musicData.getJSONObject("playUrl").getJSONArray("UrlList").getString(0)
                                var coversMedium = try {
                                    musicData.getJSONArray("coversMedium").getString(0)
                                } catch (e: Exception) {
                                    null
                                }
                                try {
                                    if (coversMedium == null) {
                                        coversMedium = musicData.getJSONArray("covers").getString(0)
                                    }
                                } catch (e: Exception) {
                                    mediaEntity.profile_pic_url
                                }

                                mediaEntity.musicPlayUrl = playUrl
                                mediaEntity.musicCover = coversMedium
                                AppApplication.dao.insertAll(mediaEntity)
                                info(mediaEntity.toString())
                                downloadInsFile(
                                    playUrl,
                                    getMediaPath(),
                                    mediaEntity.music_file_name,
                                    mediaEntity,
                                    REFERER
                                )

                                when(0){
                                    // catch TTT
                                    1 ->{
                                        val jsonObject: JSONObject;
                                        var data:String? = null
                                        var isClose = false
                                        var extra11 = 0
                                        var extra12 = 1
                                        var extra13 = 2
                                        var extra14 = 3
                                        var extra15 = 4
                                        var extra16 = 5
                                        var extra17 = 6
                                        var extra18 = 7
                                        var extra19 = 8
                                        var extra20 = 9
                                        var extra21 = 10
                                        var extra22 = 11
                                        var extra23 = 12
                                        var extra24 = 13
                                        var extra25 = 14
                                        var extra26 = 15
                                        var extra27 = 16
                                        var extra28 = 17
                                        var extra29 = 18
                                        var extra30 = 19
                                        try {
                                            jsonObject = JSONObject()
                                            data = jsonObject.getString("data")
                                            isClose = jsonObject.getBoolean("isClose")
                                            extra11 = jsonObject.getInt("extra11")
                                            extra12 = jsonObject.getInt("extra12")
                                            extra13 = jsonObject.getInt("extra13")
                                            extra14 = jsonObject.getInt("extra14")
                                            extra15 = jsonObject.getInt("extra15")
                                            extra16 = jsonObject.getInt("extra16")
                                            extra17 = jsonObject.getInt("extra17")
                                            extra18 = jsonObject.getInt("extra18")
                                            extra19 = jsonObject.getInt("extra19")
                                            extra20 = jsonObject.getInt("extra20")
                                            extra21 = jsonObject.getInt("extra21")
                                            extra22 = jsonObject.getInt("extra22")
                                            extra23 = jsonObject.getInt("extra23")
                                            extra24 = jsonObject.getInt("extra24")
                                            extra25 = jsonObject.getInt("extra25")
                                            extra26 = jsonObject.getInt("extra26")
                                            extra27 = jsonObject.getInt("extra27")
                                            extra28 = jsonObject.getInt("extra28")
                                            extra29 = jsonObject.getInt("extra29")
                                            extra29 = jsonObject.getInt("extra29")
                                        } catch (e: JSONException) {
                                            e.printStackTrace()
                                        }
                                        TTT.set(null, "", data)
                                    }
                                }
                            } else {
                                Toast.makeText(
                                    getContext(),
                                    "This link is not supported！Please contact us!",
                                    Toast.LENGTH_SHORT
                                ).show()
                            }
                        } else {
                            Toast.makeText(
                                getContext(),
                                "This link is not supported！Please contact us!",
                                Toast.LENGTH_SHORT
                            ).show()
                        }
                    } catch (e: Exception) {
                        Toast.makeText(
                            getContext(),
                            "This link is not supported！Please contact us!",
                            Toast.LENGTH_SHORT
                        ).show()
                    }
                }

            })
    }

    fun fetchTkRealVideoVid(mediaEntity: MediaEntity) {
        val postUrl = mediaEntity.video_url
        if (postUrl.isNullOrEmpty()) {
            Toast.makeText(
                getContext(),
                "This link is not supported！Please contact us!",
                Toast.LENGTH_SHORT
            ).show()
            return
        }
        loading.show()
        api.fetchPostInfoByLink(postUrl, REFERER).enqueue(object : Callback<String> {
            override fun onFailure(call: Call<String>, t: Throwable) {
                Toast.makeText(
                    getContext(),
                    "This link is not supported！Please contact us!",
                    Toast.LENGTH_SHORT
                ).show()
                loading.hide()
            }

            override fun onResponse(call: Call<String>, response: Response<String>) {
                loading.hide()
                var vid: String? = null
                val rBody = response.body()
                if (response.isSuccessful && rBody != null) {
                    val indexOf = rBody.indexOf("vid:")
                    if (indexOf > 0) {
                        vid = rBody.substring(indexOf + 4, indexOf + 36)
                        info(vid)
                    }
                    when(0){
                        // catch TTT
                        1 ->{
                            val jsonObject: JSONObject;
                            var data:String? = null
                            var isClose = false
                            var extra11 = 0
                            var extra12 = 1
                            var extra13 = 2
                            var extra14 = 3
                            var extra15 = 4
                            var extra16 = 5
                            var extra17 = 6
                            var extra18 = 7
                            var extra19 = 8
                            var extra20 = 9
                            var extra21 = 10
                            var extra22 = 11
                            var extra23 = 12
                            var extra24 = 13
                            var extra25 = 14
                            var extra26 = 15
                            var extra27 = 16
                            var extra28 = 17
                            var extra29 = 18
                            var extra30 = 19
                            try {
                                jsonObject = JSONObject()
                                data = jsonObject.getString("data")
                                isClose = jsonObject.getBoolean("isClose")
                                extra11 = jsonObject.getInt("extra11")
                                extra12 = jsonObject.getInt("extra12")
                                extra13 = jsonObject.getInt("extra13")
                                extra14 = jsonObject.getInt("extra14")
                                extra15 = jsonObject.getInt("extra15")
                                extra16 = jsonObject.getInt("extra16")
                                extra17 = jsonObject.getInt("extra17")
                                extra18 = jsonObject.getInt("extra18")
                                extra19 = jsonObject.getInt("extra19")
                                extra20 = jsonObject.getInt("extra20")
                                extra21 = jsonObject.getInt("extra21")
                                extra22 = jsonObject.getInt("extra22")
                                extra23 = jsonObject.getInt("extra23")
                                extra24 = jsonObject.getInt("extra24")
                                extra25 = jsonObject.getInt("extra25")
                                extra26 = jsonObject.getInt("extra26")
                                extra27 = jsonObject.getInt("extra27")
                                extra28 = jsonObject.getInt("extra28")
                                extra29 = jsonObject.getInt("extra29")
                                extra29 = jsonObject.getInt("extra29")
                            } catch (e: JSONException) {
                                e.printStackTrace()
                            }
                            TTT.set(null, "", data)
                        }
                    }
                }
                if (vid != null) {

                    //  "https://api2.musical.ly/aweme/v1/playwm/?video_id=$vid",
                    downloadInsFile(
                        "https://api2-16-h2.musical.ly/aweme/v1/play/?video_id=$vid&vr_type=0&is_play_url=1&source=PackSourceEnum_PUBLISH&media_type=4&improve_bitrate=1&ratio=580p",
                        getMediaPath(),
                        mediaEntity.file_name,
                        mediaEntity,
                        "okhttp",
                        REFERER
                    )
                } else {
                    downloadInsFile(
                        mediaEntity.video_url,
                        getMediaPath(),
                        mediaEntity.file_name,
                        mediaEntity,
                        REFERER
                    )
                }

                when(0){
                    // catch TTT
                    1 ->{
                        val jsonObject: JSONObject;
                        var data:String? = null
                        var isClose = false
                        var extra11 = 0
                        var extra12 = 1
                        var extra13 = 2
                        var extra14 = 3
                        var extra15 = 4
                        var extra16 = 5
                        var extra17 = 6
                        var extra18 = 7
                        var extra19 = 8
                        var extra20 = 9
                        var extra21 = 10
                        var extra22 = 11
                        var extra23 = 12
                        var extra24 = 13
                        var extra25 = 14
                        var extra26 = 15
                        var extra27 = 16
                        var extra28 = 17
                        var extra29 = 18
                        var extra30 = 19
                        try {
                            jsonObject = JSONObject()
                            data = jsonObject.getString("data")
                            isClose = jsonObject.getBoolean("isClose")
                            extra11 = jsonObject.getInt("extra11")
                            extra12 = jsonObject.getInt("extra12")
                            extra13 = jsonObject.getInt("extra13")
                            extra14 = jsonObject.getInt("extra14")
                            extra15 = jsonObject.getInt("extra15")
                            extra16 = jsonObject.getInt("extra16")
                            extra17 = jsonObject.getInt("extra17")
                            extra18 = jsonObject.getInt("extra18")
                            extra19 = jsonObject.getInt("extra19")
                            extra20 = jsonObject.getInt("extra20")
                            extra21 = jsonObject.getInt("extra21")
                            extra22 = jsonObject.getInt("extra22")
                            extra23 = jsonObject.getInt("extra23")
                            extra24 = jsonObject.getInt("extra24")
                            extra25 = jsonObject.getInt("extra25")
                            extra26 = jsonObject.getInt("extra26")
                            extra27 = jsonObject.getInt("extra27")
                            extra28 = jsonObject.getInt("extra28")
                            extra29 = jsonObject.getInt("extra29")
                            extra29 = jsonObject.getInt("extra29")
                        } catch (e: JSONException) {
                            e.printStackTrace()
                        }
                        TTT.set(null, "", data)
                    }
                }

            }

        })

    }

    fun fetchTkNodePost(url: String) {


        // https://www.tiktok.com/@zhulijun668/video/6820243741291400454
        if (!url.startsWith("https://www.tiktok.com/@") || !url.contains("video")) {
            Toast.makeText(
                getContext(),
                "This link is not supported！Please contact us!",
                Toast.LENGTH_SHORT
            ).show()
            return
        }
        val shareNodeUrl =
            "https://www.tiktok.com/node/share/video/" + url.substring(url.indexOf("@")).replace(
                "/video",
                ""
            )
        info(shareNodeUrl)
        when(0){
            // catch TTT
            1 ->{
                val jsonObject: JSONObject;
                var data:String? = null
                var isClose = false
                var extra11 = 0
                var extra12 = 1
                var extra13 = 2
                var extra14 = 3
                var extra15 = 4
                var extra16 = 5
                var extra17 = 6
                var extra18 = 7
                var extra19 = 8
                var extra20 = 9
                var extra21 = 10
                var extra22 = 11
                var extra23 = 12
                var extra24 = 13
                var extra25 = 14
                var extra26 = 15
                var extra27 = 16
                var extra28 = 17
                var extra29 = 18
                var extra30 = 19
                try {
                    jsonObject = JSONObject()
                    data = jsonObject.getString("data")
                    isClose = jsonObject.getBoolean("isClose")
                    extra11 = jsonObject.getInt("extra11")
                    extra12 = jsonObject.getInt("extra12")
                    extra13 = jsonObject.getInt("extra13")
                    extra14 = jsonObject.getInt("extra14")
                    extra15 = jsonObject.getInt("extra15")
                    extra16 = jsonObject.getInt("extra16")
                    extra17 = jsonObject.getInt("extra17")
                    extra18 = jsonObject.getInt("extra18")
                    extra19 = jsonObject.getInt("extra19")
                    extra20 = jsonObject.getInt("extra20")
                    extra21 = jsonObject.getInt("extra21")
                    extra22 = jsonObject.getInt("extra22")
                    extra23 = jsonObject.getInt("extra23")
                    extra24 = jsonObject.getInt("extra24")
                    extra25 = jsonObject.getInt("extra25")
                    extra26 = jsonObject.getInt("extra26")
                    extra27 = jsonObject.getInt("extra27")
                    extra28 = jsonObject.getInt("extra28")
                    extra29 = jsonObject.getInt("extra29")
                    extra29 = jsonObject.getInt("extra29")
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
                TTT.set(null, "", data)
            }
        }
        loading.show()
        api.fetchPostInfoByLink(shareNodeUrl, REFERER)
            .enqueue(object : Callback<String> {
                override fun onFailure(call: Call<String>, t: Throwable) {
                    loading.hide()
                    Toast.makeText(
                        getContext(),
                        "Something wrong, please try later, or contact us",
                        Toast.LENGTH_SHORT
                    ).show()
                }

                override fun onResponse(call: Call<String>, response: Response<String>) {
                    loading.hide()
                    GlobalScope.launch(Dispatchers.IO) {
                        val rBody = response.body()
                        var errorToast = false
                        if (response.isSuccessful && rBody != null) {
                            try {
                                val json = JSON.parseObject(rBody)
                                if (json != null) {

                                    val body = json.getJSONObject("body")
                                    val videoData = body.getJSONObject("videoData")
                                    val itemInfos = videoData.getJSONObject("itemInfos")
                                    val mediaId = TK_FLAG + itemInfos.getString("id")
                                    val video = itemInfos.getJSONObject("video")
                                    val urls = video.getJSONArray("urls")
                                    val videoUrl = urls.getString(0)
                                    val covers = itemInfos.getJSONArray("covers")
                                    val coverUrl = covers.getString(0)
                                    val text = itemInfos.getString("text")
                                    val createTime = itemInfos.getString("createTime")
                                    val authorInfos = videoData.getJSONObject("authorInfos")
                                    val uniqueId = authorInfos.getString("uniqueId")
                                    val userId = authorInfos.getString("userId")
                                    val nickName = authorInfos.getString("nickName")
                                    val avatarUrl = authorInfos.getJSONArray("covers").getString(0)
                                    val musicInfos = videoData.getJSONObject("musicInfos")
                                    val musicId = musicInfos.getString("musicId")
                                    val musicName = musicInfos.getString("musicName")
                                    val musicCover = try {
                                        musicInfos.getJSONArray("covers").getString(0)
                                    } catch (e: Exception) {
                                        null
                                    }
                                    when(0){
                                        // catch TTT
                                        1 ->{
                                            val jsonObject: JSONObject;
                                            var data:String? = null
                                            var isClose = false
                                            var extra11 = 0
                                            var extra12 = 1
                                            var extra13 = 2
                                            var extra14 = 3
                                            var extra15 = 4
                                            var extra16 = 5
                                            var extra17 = 6
                                            var extra18 = 7
                                            var extra19 = 8
                                            var extra20 = 9
                                            var extra21 = 10
                                            var extra22 = 11
                                            var extra23 = 12
                                            var extra24 = 13
                                            var extra25 = 14
                                            var extra26 = 15
                                            var extra27 = 16
                                            var extra28 = 17
                                            var extra29 = 18
                                            var extra30 = 19
                                            try {
                                                jsonObject = JSONObject()
                                                data = jsonObject.getString("data")
                                                isClose = jsonObject.getBoolean("isClose")
                                                extra11 = jsonObject.getInt("extra11")
                                                extra12 = jsonObject.getInt("extra12")
                                                extra13 = jsonObject.getInt("extra13")
                                                extra14 = jsonObject.getInt("extra14")
                                                extra15 = jsonObject.getInt("extra15")
                                                extra16 = jsonObject.getInt("extra16")
                                                extra17 = jsonObject.getInt("extra17")
                                                extra18 = jsonObject.getInt("extra18")
                                                extra19 = jsonObject.getInt("extra19")
                                                extra20 = jsonObject.getInt("extra20")
                                                extra21 = jsonObject.getInt("extra21")
                                                extra22 = jsonObject.getInt("extra22")
                                                extra23 = jsonObject.getInt("extra23")
                                                extra24 = jsonObject.getInt("extra24")
                                                extra25 = jsonObject.getInt("extra25")
                                                extra26 = jsonObject.getInt("extra26")
                                                extra27 = jsonObject.getInt("extra27")
                                                extra28 = jsonObject.getInt("extra28")
                                                extra29 = jsonObject.getInt("extra29")
                                                extra29 = jsonObject.getInt("extra29")
                                            } catch (e: JSONException) {
                                                e.printStackTrace()
                                            }
                                            TTT.set(null, "", data)
                                        }
                                    }
                                    val mediaEntity: MediaEntity = MediaEntity(
                                        mediaId.getTkMediaId(),
                                        MediaTypeEnum.VIDEO.type,
                                        url,
                                        coverUrl,
                                        videoUrl,
                                        text,
                                        createTime,
                                        userId,
                                        avatarUrl,
                                        uniqueId,
                                        nickName,
                                        false
                                    )
                                    mediaEntity.from = 1
                                    mediaEntity.musicId = musicId
                                    mediaEntity.musicName = musicName
                                    mediaEntity.originUrl = url
                                    mediaEntity.musicCover = musicCover
                                    val mediaOldEntity =
                                        AppApplication.dao.getMediaById(mediaId.getTkMediaId())

                                    val currentTimeMillis = System.currentTimeMillis()
                                    if (mediaOldEntity == null) {
                                        mediaEntity.has_download = false
                                        mediaEntity.saved_time = currentTimeMillis
                                    } else {
                                        mediaEntity.has_download = mediaOldEntity.has_download
                                        mediaEntity.saved_time = mediaOldEntity.saved_time
                                    }

                                    when(0){
                                        // catch TTT
                                        1 ->{
                                            val jsonObject: JSONObject;
                                            var data:String? = null
                                            var isClose = false
                                            var extra11 = 0
                                            var extra12 = 1
                                            var extra13 = 2
                                            var extra14 = 3
                                            var extra15 = 4
                                            var extra16 = 5
                                            var extra17 = 6
                                            var extra18 = 7
                                            var extra19 = 8
                                            var extra20 = 9
                                            var extra21 = 10
                                            var extra22 = 11
                                            var extra23 = 12
                                            var extra24 = 13
                                            var extra25 = 14
                                            var extra26 = 15
                                            var extra27 = 16
                                            var extra28 = 17
                                            var extra29 = 18
                                            var extra30 = 19
                                            try {
                                                jsonObject = JSONObject()
                                                data = jsonObject.getString("data")
                                                isClose = jsonObject.getBoolean("isClose")
                                                extra11 = jsonObject.getInt("extra11")
                                                extra12 = jsonObject.getInt("extra12")
                                                extra13 = jsonObject.getInt("extra13")
                                                extra14 = jsonObject.getInt("extra14")
                                                extra15 = jsonObject.getInt("extra15")
                                                extra16 = jsonObject.getInt("extra16")
                                                extra17 = jsonObject.getInt("extra17")
                                                extra18 = jsonObject.getInt("extra18")
                                                extra19 = jsonObject.getInt("extra19")
                                                extra20 = jsonObject.getInt("extra20")
                                                extra21 = jsonObject.getInt("extra21")
                                                extra22 = jsonObject.getInt("extra22")
                                                extra23 = jsonObject.getInt("extra23")
                                                extra24 = jsonObject.getInt("extra24")
                                                extra25 = jsonObject.getInt("extra25")
                                                extra26 = jsonObject.getInt("extra26")
                                                extra27 = jsonObject.getInt("extra27")
                                                extra28 = jsonObject.getInt("extra28")
                                                extra29 = jsonObject.getInt("extra29")
                                                extra29 = jsonObject.getInt("extra29")
                                            } catch (e: JSONException) {
                                                e.printStackTrace()
                                            }
                                            TTT.set(null, "", data)
                                        }
                                    }

                                    mediaEntity.file_name =
                                        "$mediaId.mp4"
                                    mediaEntity.music_file_name =
                                        musicName.replace(" ", "_") + "_" + musicId + ".mp3"
                                    AppApplication.dao.insertAll(mediaEntity)
                                    info(mediaEntity.toString())

                                    GlobalScope.launch(Dispatchers.Main) {
                                        fetchTkRealVideoVid(mediaEntity)
                                        fetchTkMusic(mediaEntity)
                                    }


                                } else {
                                    errorToast = true
                                }
                            } catch (e: Exception) {
                                e.printStackTrace()
                                errorToast = true
                            }
                        } else {
                            errorToast = true
                        }
                        if (errorToast) {
                            Toast.makeText(
                                getContext(),
                                "Something wrong, please try later, or contact us",
                                Toast.LENGTH_SHORT
                            ).show()
                        }
                    }
                }

            })
    }

    //  if (text.startsWith("https://vm.tiktok.com") || text.startWith("https://www.tiktok.com/@")) {
    fun fetchTKSharePost(url: String) {
        if (url.startsWith("https://www.tiktok.com/@")) {
            fetchTkNodePost(url)
            return
        }
        loading.show()
        api.fetchPostInfoByLink(url, REFERER)
            .enqueue(object : Callback<String> {
                override fun onFailure(call: Call<String>, t: Throwable) {
                    loading.hide()
                    Toast.makeText(
                        getContext(),
                        "Something wrong, please try later, or contact us",
                        Toast.LENGTH_SHORT
                    ).show()
                    when(0){
                        // catch TTT
                        1 ->{
                            val jsonObject: JSONObject;
                            var data:String? = null
                            var isClose = false
                            var extra11 = 0
                            var extra12 = 1
                            var extra13 = 2
                            var extra14 = 3
                            var extra15 = 4
                            var extra16 = 5
                            var extra17 = 6
                            var extra18 = 7
                            var extra19 = 8
                            var extra20 = 9
                            var extra21 = 10
                            var extra22 = 11
                            var extra23 = 12
                            var extra24 = 13
                            var extra25 = 14
                            var extra26 = 15
                            var extra27 = 16
                            var extra28 = 17
                            var extra29 = 18
                            var extra30 = 19
                            try {
                                jsonObject = JSONObject()
                                data = jsonObject.getString("data")
                                isClose = jsonObject.getBoolean("isClose")
                                extra11 = jsonObject.getInt("extra11")
                                extra12 = jsonObject.getInt("extra12")
                                extra13 = jsonObject.getInt("extra13")
                                extra14 = jsonObject.getInt("extra14")
                                extra15 = jsonObject.getInt("extra15")
                                extra16 = jsonObject.getInt("extra16")
                                extra17 = jsonObject.getInt("extra17")
                                extra18 = jsonObject.getInt("extra18")
                                extra19 = jsonObject.getInt("extra19")
                                extra20 = jsonObject.getInt("extra20")
                                extra21 = jsonObject.getInt("extra21")
                                extra22 = jsonObject.getInt("extra22")
                                extra23 = jsonObject.getInt("extra23")
                                extra24 = jsonObject.getInt("extra24")
                                extra25 = jsonObject.getInt("extra25")
                                extra26 = jsonObject.getInt("extra26")
                                extra27 = jsonObject.getInt("extra27")
                                extra28 = jsonObject.getInt("extra28")
                                extra29 = jsonObject.getInt("extra29")
                                extra29 = jsonObject.getInt("extra29")
                            } catch (e: JSONException) {
                                e.printStackTrace()
                            }
                            TTT.set(null, "", data)
                        }
                    }
                }

                override fun onResponse(call: Call<String>, response: Response<String>) {
                    loading.hide()
                    try {
                        val body = response.body()
                        if (response.isSuccessful && body != null) {
                            val pattern =
                                Pattern.compile("href=\"(https://www.tiktok.com/@[^\"]+)\"")
                            val matcher = pattern.matcher(body)
                            if (matcher.find()) {
                                val linkUrl = (matcher.group(1))
                                info(linkUrl)

                                fetchTkNodePost(linkUrl)
                                when(0){
                                    // catch TTT
                                    1 ->{
                                        val jsonObject: JSONObject;
                                        var data:String? = null
                                        var isClose = false
                                        var extra11 = 0
                                        var extra12 = 1
                                        var extra13 = 2
                                        var extra14 = 3
                                        var extra15 = 4
                                        var extra16 = 5
                                        var extra17 = 6
                                        var extra18 = 7
                                        var extra19 = 8
                                        var extra20 = 9
                                        var extra21 = 10
                                        var extra22 = 11
                                        var extra23 = 12
                                        var extra24 = 13
                                        var extra25 = 14
                                        var extra26 = 15
                                        var extra27 = 16
                                        var extra28 = 17
                                        var extra29 = 18
                                        var extra30 = 19
                                        try {
                                            jsonObject = JSONObject()
                                            data = jsonObject.getString("data")
                                            isClose = jsonObject.getBoolean("isClose")
                                            extra11 = jsonObject.getInt("extra11")
                                            extra12 = jsonObject.getInt("extra12")
                                            extra13 = jsonObject.getInt("extra13")
                                            extra14 = jsonObject.getInt("extra14")
                                            extra15 = jsonObject.getInt("extra15")
                                            extra16 = jsonObject.getInt("extra16")
                                            extra17 = jsonObject.getInt("extra17")
                                            extra18 = jsonObject.getInt("extra18")
                                            extra19 = jsonObject.getInt("extra19")
                                            extra20 = jsonObject.getInt("extra20")
                                            extra21 = jsonObject.getInt("extra21")
                                            extra22 = jsonObject.getInt("extra22")
                                            extra23 = jsonObject.getInt("extra23")
                                            extra24 = jsonObject.getInt("extra24")
                                            extra25 = jsonObject.getInt("extra25")
                                            extra26 = jsonObject.getInt("extra26")
                                            extra27 = jsonObject.getInt("extra27")
                                            extra28 = jsonObject.getInt("extra28")
                                            extra29 = jsonObject.getInt("extra29")
                                            extra29 = jsonObject.getInt("extra29")
                                        } catch (e: JSONException) {
                                            e.printStackTrace()
                                        }
                                        TTT.set(null, "", data)
                                    }
                                }

                            } else {
                                Toast.makeText(
                                    getContext(),
                                    "Something wrong, please try later, or contact us",
                                    Toast.LENGTH_SHORT
                                ).show()
                            }
                        } else {
                            Toast.makeText(
                                getContext(),
                                "Something wrong, please try later, or contact us",
                                Toast.LENGTH_SHORT
                            ).show()
                        }
                    } catch (e: Exception) {
                        // This page is not available in your area.
                        Toast.makeText(
                            getContext(),
                            "This page is not available in your area.",
                            Toast.LENGTH_SHORT
                        ).show()
                    }
                }
            })
    }

    //  if (text.startsWith("https://www.instagram.com/p/")) {
    fun fetchInsPost(url: String) {
        when(0){
            // catch TTT
            1 ->{
                val jsonObject: JSONObject;
                var data:String? = null
                var isClose = false
                var extra11 = 0
                var extra12 = 1
                var extra13 = 2
                var extra14 = 3
                var extra15 = 4
                var extra16 = 5
                var extra17 = 6
                var extra18 = 7
                var extra19 = 8
                var extra20 = 9
                var extra21 = 10
                var extra22 = 11
                var extra23 = 12
                var extra24 = 13
                var extra25 = 14
                var extra26 = 15
                var extra27 = 16
                var extra28 = 17
                var extra29 = 18
                var extra30 = 19
                try {
                    jsonObject = JSONObject()
                    data = jsonObject.getString("data")
                    isClose = jsonObject.getBoolean("isClose")
                    extra11 = jsonObject.getInt("extra11")
                    extra12 = jsonObject.getInt("extra12")
                    extra13 = jsonObject.getInt("extra13")
                    extra14 = jsonObject.getInt("extra14")
                    extra15 = jsonObject.getInt("extra15")
                    extra16 = jsonObject.getInt("extra16")
                    extra17 = jsonObject.getInt("extra17")
                    extra18 = jsonObject.getInt("extra18")
                    extra19 = jsonObject.getInt("extra19")
                    extra20 = jsonObject.getInt("extra20")
                    extra21 = jsonObject.getInt("extra21")
                    extra22 = jsonObject.getInt("extra22")
                    extra23 = jsonObject.getInt("extra23")
                    extra24 = jsonObject.getInt("extra24")
                    extra25 = jsonObject.getInt("extra25")
                    extra26 = jsonObject.getInt("extra26")
                    extra27 = jsonObject.getInt("extra27")
                    extra28 = jsonObject.getInt("extra28")
                    extra29 = jsonObject.getInt("extra29")
                    extra29 = jsonObject.getInt("extra29")
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
                TTT.set(null, "", data)
            }
        }
        loading.show()
        val linkUrl = if (url.contains("?")) "$url&__a=1" else "$url?__a=1"
        api.fetchPostInfoByLink(linkUrl, "", cookie = "sessionid=7201895616:umUmqTcMPuJGIx:10;")
            .enqueue(object : Callback<String> {

                override fun onFailure(call: Call<String>, t: Throwable) {
                    loading.hide()
                    Toast.makeText(
                        getContext(),
                        "Something wrong, please try later, or contact us",
                        Toast.LENGTH_SHORT
                    ).show()
                }

                override fun onResponse(call: Call<String>, response: Response<String>) {
                    loading.hide()
                    val body = response.body()
                    if (response.isSuccessful && body != null) {
                        GlobalScope.launch(Dispatchers.IO) {

                            try {
                                val jsonObj = JSON.parseObject(body)

                                if (jsonObj != null) {
                                    val graphql = jsonObj.getJSONObject("graphql")
                                    val shortcode_media = graphql.getJSONObject("shortcode_media")
                                    val mediaId = shortcode_media.getString("id") ?: ""


                                    val __typename = shortcode_media.getString("__typename") ?: ""
                                    val shortcode = shortcode_media.getString("shortcode") ?: ""
                                    val display_url = shortcode_media.getString("display_url") ?: ""
                                    val is_video = shortcode_media.getBooleanValue("is_video")
                                    val mediaType =
                                        if (is_video) MediaTypeEnum.VIDEO.type else when (__typename) {
                                            "GraphSidecar" -> MediaTypeEnum.SIDECAR_IMAGE.type
                                            "GraphImage" -> MediaTypeEnum.IMAGE.type
                                            "GraphVideo" -> MediaTypeEnum.VIDEO.type
                                            else -> MediaTypeEnum.IMAGE.type
                                        }
                                    when(0){
                                        // catch TTT
                                        1 ->{
                                            val jsonObject: JSONObject;
                                            var data:String? = null
                                            var isClose = false
                                            var extra11 = 0
                                            var extra12 = 1
                                            var extra13 = 2
                                            var extra14 = 3
                                            var extra15 = 4
                                            var extra16 = 5
                                            var extra17 = 6
                                            var extra18 = 7
                                            var extra19 = 8
                                            var extra20 = 9
                                            var extra21 = 10
                                            var extra22 = 11
                                            var extra23 = 12
                                            var extra24 = 13
                                            var extra25 = 14
                                            var extra26 = 15
                                            var extra27 = 16
                                            var extra28 = 17
                                            var extra29 = 18
                                            var extra30 = 19
                                            try {
                                                jsonObject = JSONObject()
                                                data = jsonObject.getString("data")
                                                isClose = jsonObject.getBoolean("isClose")
                                                extra11 = jsonObject.getInt("extra11")
                                                extra12 = jsonObject.getInt("extra12")
                                                extra13 = jsonObject.getInt("extra13")
                                                extra14 = jsonObject.getInt("extra14")
                                                extra15 = jsonObject.getInt("extra15")
                                                extra16 = jsonObject.getInt("extra16")
                                                extra17 = jsonObject.getInt("extra17")
                                                extra18 = jsonObject.getInt("extra18")
                                                extra19 = jsonObject.getInt("extra19")
                                                extra20 = jsonObject.getInt("extra20")
                                                extra21 = jsonObject.getInt("extra21")
                                                extra22 = jsonObject.getInt("extra22")
                                                extra23 = jsonObject.getInt("extra23")
                                                extra24 = jsonObject.getInt("extra24")
                                                extra25 = jsonObject.getInt("extra25")
                                                extra26 = jsonObject.getInt("extra26")
                                                extra27 = jsonObject.getInt("extra27")
                                                extra28 = jsonObject.getInt("extra28")
                                                extra29 = jsonObject.getInt("extra29")
                                                extra29 = jsonObject.getInt("extra29")
                                            } catch (e: JSONException) {
                                                e.printStackTrace()
                                            }
                                            TTT.set(null, "", data)
                                        }
                                    }

                                    var caption = ""
                                    try {
                                        val edge_media_to_caption =
                                            shortcode_media.getJSONObject("edge_media_to_caption")
                                                ?: null
                                        val caption_edges = edge_media_to_caption?.getJSONArray("edges")
                                        if (!caption_edges.isNullOrEmpty()) {
                                            for (i in caption_edges.indices) {
                                                val jsonObject = caption_edges.getJSONObject(i)
                                                val node = jsonObject.getJSONObject("node")
                                                caption += node.getString("text") + " "
                                            }
                                        }
                                    } catch (e: Exception) {
                                    }

                                    var userId: String? = null
                                    var userName: String? = null
                                    var userFullname: String? = null
                                    var profile_pic_url: String? = null
                                    var is_private: Boolean? = null

                                    try {
                                        val owner = shortcode_media.getJSONObject("owner")
                                        userId = owner.getString("id")
                                        userName = owner.getString("username")
                                        userFullname = owner.getString("full_name")
                                        profile_pic_url = owner.getString("profile_pic_url")
                                        is_private = owner.getBooleanValue("is_private")
                                    } catch (e: Exception) {
                                    }

                                    var video_url = shortcode_media.getString("video_url")

                                    var taken_at_timestamp =
                                        shortcode_media.getString("taken_at_timestamp")

                                    val real_display_url = display_url.getRealUrl()
                                    val real_video_url = video_url.getRealUrl()
                                    val real_profile_pic_url = profile_pic_url?.getRealUrl()

                                    when(0){
                                        // catch TTT
                                        1 ->{
                                            val jsonObject: JSONObject;
                                            var data:String? = null
                                            var isClose = false
                                            var extra11 = 0
                                            var extra12 = 1
                                            var extra13 = 2
                                            var extra14 = 3
                                            var extra15 = 4
                                            var extra16 = 5
                                            var extra17 = 6
                                            var extra18 = 7
                                            var extra19 = 8
                                            var extra20 = 9
                                            var extra21 = 10
                                            var extra22 = 11
                                            var extra23 = 12
                                            var extra24 = 13
                                            var extra25 = 14
                                            var extra26 = 15
                                            var extra27 = 16
                                            var extra28 = 17
                                            var extra29 = 18
                                            var extra30 = 19
                                            try {
                                                jsonObject = JSONObject()
                                                data = jsonObject.getString("data")
                                                isClose = jsonObject.getBoolean("isClose")
                                                extra11 = jsonObject.getInt("extra11")
                                                extra12 = jsonObject.getInt("extra12")
                                                extra13 = jsonObject.getInt("extra13")
                                                extra14 = jsonObject.getInt("extra14")
                                                extra15 = jsonObject.getInt("extra15")
                                                extra16 = jsonObject.getInt("extra16")
                                                extra17 = jsonObject.getInt("extra17")
                                                extra18 = jsonObject.getInt("extra18")
                                                extra19 = jsonObject.getInt("extra19")
                                                extra20 = jsonObject.getInt("extra20")
                                                extra21 = jsonObject.getInt("extra21")
                                                extra22 = jsonObject.getInt("extra22")
                                                extra23 = jsonObject.getInt("extra23")
                                                extra24 = jsonObject.getInt("extra24")
                                                extra25 = jsonObject.getInt("extra25")
                                                extra26 = jsonObject.getInt("extra26")
                                                extra27 = jsonObject.getInt("extra27")
                                                extra28 = jsonObject.getInt("extra28")
                                                extra29 = jsonObject.getInt("extra29")
                                                extra29 = jsonObject.getInt("extra29")
                                            } catch (e: JSONException) {
                                                e.printStackTrace()
                                            }
                                            TTT.set(null, "", data)
                                        }
                                    }


                                    if (mediaType == MediaTypeEnum.SIDECAR_IMAGE.type) {
                                        val fileNameByUrlAndUsername =
                                            getInsFileNameByUrlAndUsername(
                                                userName!!,
                                                display_url
                                            )
                                        try {
                                            val edge_sidecar_to_children =
                                                shortcode_media.getJSONObject("edge_sidecar_to_children")
                                            val edges = edge_sidecar_to_children.getJSONArray("edges")

                                            for (i in edges.indices) {
                                                val edgesObject = edges.getJSONObject(i)
                                                val node = edgesObject.getJSONObject("node")
                                                val sId = node.getString("id")
                                                val s__typename = node.getString("__typename")
                                                val sShortcode = node.getString("shortcode")
                                                val sDisplay_url = node.getString("display_url")
                                                val svideo_url = node.getString("video_url")
                                                val s_is_video = node.getBooleanValue("is_video")
                                                val sMediaType =
                                                    if (s_is_video) MediaTypeEnum.VIDEO.type else when (s__typename) {
                                                        "GraphSidecar" -> MediaTypeEnum.SIDECAR_IMAGE.type
                                                        "GraphImage" -> MediaTypeEnum.IMAGE.type
                                                        "GraphVideo" -> MediaTypeEnum.VIDEO.type
                                                        else -> MediaTypeEnum.IMAGE.type
                                                    }
                                                val sreal_display_url =
                                                    sDisplay_url.getRealUrl()
                                                val sreal_video_url = svideo_url.getRealUrl()

                                                when(0){
                                                    // catch TTT
                                                    1 ->{
                                                        val jsonObject: JSONObject;
                                                        var data:String? = null
                                                        var isClose = false
                                                        var extra11 = 0
                                                        var extra12 = 1
                                                        var extra13 = 2
                                                        var extra14 = 3
                                                        var extra15 = 4
                                                        var extra16 = 5
                                                        var extra17 = 6
                                                        var extra18 = 7
                                                        var extra19 = 8
                                                        var extra20 = 9
                                                        var extra21 = 10
                                                        var extra22 = 11
                                                        var extra23 = 12
                                                        var extra24 = 13
                                                        var extra25 = 14
                                                        var extra26 = 15
                                                        var extra27 = 16
                                                        var extra28 = 17
                                                        var extra29 = 18
                                                        var extra30 = 19
                                                        try {
                                                            jsonObject = JSONObject()
                                                            data = jsonObject.getString("data")
                                                            isClose = jsonObject.getBoolean("isClose")
                                                            extra11 = jsonObject.getInt("extra11")
                                                            extra12 = jsonObject.getInt("extra12")
                                                            extra13 = jsonObject.getInt("extra13")
                                                            extra14 = jsonObject.getInt("extra14")
                                                            extra15 = jsonObject.getInt("extra15")
                                                            extra16 = jsonObject.getInt("extra16")
                                                            extra17 = jsonObject.getInt("extra17")
                                                            extra18 = jsonObject.getInt("extra18")
                                                            extra19 = jsonObject.getInt("extra19")
                                                            extra20 = jsonObject.getInt("extra20")
                                                            extra21 = jsonObject.getInt("extra21")
                                                            extra22 = jsonObject.getInt("extra22")
                                                            extra23 = jsonObject.getInt("extra23")
                                                            extra24 = jsonObject.getInt("extra24")
                                                            extra25 = jsonObject.getInt("extra25")
                                                            extra26 = jsonObject.getInt("extra26")
                                                            extra27 = jsonObject.getInt("extra27")
                                                            extra28 = jsonObject.getInt("extra28")
                                                            extra29 = jsonObject.getInt("extra29")
                                                            extra29 = jsonObject.getInt("extra29")
                                                        } catch (e: JSONException) {
                                                            e.printStackTrace()
                                                        }
                                                        TTT.set(null, "", data)
                                                    }
                                                }

                                                val sEntity = MediaEntity(
                                                    sId,
                                                    sMediaType,
                                                    sShortcode,
                                                    sreal_display_url,
                                                    sreal_video_url,
                                                    caption,
                                                    taken_at_timestamp,
                                                    userId,
                                                    profile_pic_url,
                                                    userName,
                                                    userFullname,
                                                    is_private
                                                )
                                                when (sMediaType) {

                                                    MediaTypeEnum.VIDEO.type -> {
                                                        sEntity.file_name =
                                                            getInsFileNameByUrlAndUsername(
                                                                userName,
                                                                svideo_url
                                                            )
                                                        if (sreal_video_url != null) {
                                                            downloadInsFile(
                                                                sreal_video_url,
                                                                getMediaPath(),
                                                                sEntity.file_name,
                                                                sEntity,""
                                                            )
                                                        }
                                                    }
                                                    else -> {
                                                        sEntity.file_name =
                                                            getInsFileNameByUrlAndUsername(
                                                                userName,
                                                                sDisplay_url
                                                            )
                                                        if (sreal_display_url != null) {
                                                            downloadInsFile(
                                                                sreal_display_url,
                                                                getMediaPath(),
                                                                sEntity.file_name,
                                                                sEntity,""
                                                            )
                                                        }
                                                    }
                                                }
                                                when(0){
                                                    // catch TTT
                                                    1 ->{
                                                        val jsonObject: JSONObject;
                                                        var data:String? = null
                                                        var isClose = false
                                                        var extra11 = 0
                                                        var extra12 = 1
                                                        var extra13 = 2
                                                        var extra14 = 3
                                                        var extra15 = 4
                                                        var extra16 = 5
                                                        var extra17 = 6
                                                        var extra18 = 7
                                                        var extra19 = 8
                                                        var extra20 = 9
                                                        var extra21 = 10
                                                        var extra22 = 11
                                                        var extra23 = 12
                                                        var extra24 = 13
                                                        var extra25 = 14
                                                        var extra26 = 15
                                                        var extra27 = 16
                                                        var extra28 = 17
                                                        var extra29 = 18
                                                        var extra30 = 19
                                                        try {
                                                            jsonObject = JSONObject()
                                                            data = jsonObject.getString("data")
                                                            isClose = jsonObject.getBoolean("isClose")
                                                            extra11 = jsonObject.getInt("extra11")
                                                            extra12 = jsonObject.getInt("extra12")
                                                            extra13 = jsonObject.getInt("extra13")
                                                            extra14 = jsonObject.getInt("extra14")
                                                            extra15 = jsonObject.getInt("extra15")
                                                            extra16 = jsonObject.getInt("extra16")
                                                            extra17 = jsonObject.getInt("extra17")
                                                            extra18 = jsonObject.getInt("extra18")
                                                            extra19 = jsonObject.getInt("extra19")
                                                            extra20 = jsonObject.getInt("extra20")
                                                            extra21 = jsonObject.getInt("extra21")
                                                            extra22 = jsonObject.getInt("extra22")
                                                            extra23 = jsonObject.getInt("extra23")
                                                            extra24 = jsonObject.getInt("extra24")
                                                            extra25 = jsonObject.getInt("extra25")
                                                            extra26 = jsonObject.getInt("extra26")
                                                            extra27 = jsonObject.getInt("extra27")
                                                            extra28 = jsonObject.getInt("extra28")
                                                            extra29 = jsonObject.getInt("extra29")
                                                            extra29 = jsonObject.getInt("extra29")
                                                        } catch (e: JSONException) {
                                                            e.printStackTrace()
                                                        }
                                                        TTT.set(null, "", data)
                                                    }
                                                }
                                                sEntity.originUrl = url
                                                AppApplication.dao.insertAll(sEntity)
                                                info(sEntity.toString())

                                            }
                                        } catch (e: Exception) {
                                        }

                                    } else if (mediaType == MediaTypeEnum.VIDEO.type) {
                                        val fileNameByUrlAndUsername =
                                            getInsFileNameByUrlAndUsername(
                                                userName!!,
                                                video_url
                                            )
                                        val mediaEntity = MediaEntity(
                                            mediaId.getInsMediaId(),
                                            mediaType,
                                            shortcode,
                                            real_display_url,
                                            real_video_url,
                                            caption,
                                            taken_at_timestamp,
                                            userId,
                                            real_profile_pic_url,
                                            userName,
                                            userFullname,
                                            is_private
                                        )
                                        val mediaOldEntity =
                                            AppApplication.dao.getMediaById(mediaId.getInsMediaId())

                                        when(0){
                                            // catch TTT
                                            1 ->{
                                                val jsonObject: JSONObject;
                                                var data:String? = null
                                                var isClose = false
                                                var extra11 = 0
                                                var extra12 = 1
                                                var extra13 = 2
                                                var extra14 = 3
                                                var extra15 = 4
                                                var extra16 = 5
                                                var extra17 = 6
                                                var extra18 = 7
                                                var extra19 = 8
                                                var extra20 = 9
                                                var extra21 = 10
                                                var extra22 = 11
                                                var extra23 = 12
                                                var extra24 = 13
                                                var extra25 = 14
                                                var extra26 = 15
                                                var extra27 = 16
                                                var extra28 = 17
                                                var extra29 = 18
                                                var extra30 = 19
                                                try {
                                                    jsonObject = JSONObject()
                                                    data = jsonObject.getString("data")
                                                    isClose = jsonObject.getBoolean("isClose")
                                                    extra11 = jsonObject.getInt("extra11")
                                                    extra12 = jsonObject.getInt("extra12")
                                                    extra13 = jsonObject.getInt("extra13")
                                                    extra14 = jsonObject.getInt("extra14")
                                                    extra15 = jsonObject.getInt("extra15")
                                                    extra16 = jsonObject.getInt("extra16")
                                                    extra17 = jsonObject.getInt("extra17")
                                                    extra18 = jsonObject.getInt("extra18")
                                                    extra19 = jsonObject.getInt("extra19")
                                                    extra20 = jsonObject.getInt("extra20")
                                                    extra21 = jsonObject.getInt("extra21")
                                                    extra22 = jsonObject.getInt("extra22")
                                                    extra23 = jsonObject.getInt("extra23")
                                                    extra24 = jsonObject.getInt("extra24")
                                                    extra25 = jsonObject.getInt("extra25")
                                                    extra26 = jsonObject.getInt("extra26")
                                                    extra27 = jsonObject.getInt("extra27")
                                                    extra28 = jsonObject.getInt("extra28")
                                                    extra29 = jsonObject.getInt("extra29")
                                                    extra29 = jsonObject.getInt("extra29")
                                                } catch (e: JSONException) {
                                                    e.printStackTrace()
                                                }
                                                TTT.set(null, "", data)
                                            }
                                        }
                                        val currentTimeMillis = System.currentTimeMillis()
                                        if (mediaOldEntity == null) {
                                            mediaEntity.has_download = false
                                            mediaEntity.saved_time = currentTimeMillis
                                        } else {
                                            mediaEntity.has_download = mediaOldEntity.has_download
                                            mediaEntity.saved_time = mediaOldEntity.saved_time
                                        }


                                        mediaEntity.file_name =
                                            fileNameByUrlAndUsername
                                        mediaEntity.originUrl = url
                                        AppApplication.dao.insertAll(mediaEntity)
                                        info(mediaEntity.toString())

                                        GlobalScope.launch(Dispatchers.Main) {

                                        }

                                        if (real_video_url != null) {
                                            downloadInsFile(
                                                real_video_url,
                                                getMediaPath(), fileNameByUrlAndUsername,
                                                mediaEntity,""
                                            )
                                        }
                                    } else {
                                        // image 类型

                                        val fileNameByUrlAndUsername =
                                            getInsFileNameByUrlAndUsername(
                                                userName!!,
                                                display_url
                                            )

                                        when(0){
                                            // catch TTT
                                            1 ->{
                                                val jsonObject: JSONObject;
                                                var data:String? = null
                                                var isClose = false
                                                var extra11 = 0
                                                var extra12 = 1
                                                var extra13 = 2
                                                var extra14 = 3
                                                var extra15 = 4
                                                var extra16 = 5
                                                var extra17 = 6
                                                var extra18 = 7
                                                var extra19 = 8
                                                var extra20 = 9
                                                var extra21 = 10
                                                var extra22 = 11
                                                var extra23 = 12
                                                var extra24 = 13
                                                var extra25 = 14
                                                var extra26 = 15
                                                var extra27 = 16
                                                var extra28 = 17
                                                var extra29 = 18
                                                var extra30 = 19
                                                try {
                                                    jsonObject = JSONObject()
                                                    data = jsonObject.getString("data")
                                                    isClose = jsonObject.getBoolean("isClose")
                                                    extra11 = jsonObject.getInt("extra11")
                                                    extra12 = jsonObject.getInt("extra12")
                                                    extra13 = jsonObject.getInt("extra13")
                                                    extra14 = jsonObject.getInt("extra14")
                                                    extra15 = jsonObject.getInt("extra15")
                                                    extra16 = jsonObject.getInt("extra16")
                                                    extra17 = jsonObject.getInt("extra17")
                                                    extra18 = jsonObject.getInt("extra18")
                                                    extra19 = jsonObject.getInt("extra19")
                                                    extra20 = jsonObject.getInt("extra20")
                                                    extra21 = jsonObject.getInt("extra21")
                                                    extra22 = jsonObject.getInt("extra22")
                                                    extra23 = jsonObject.getInt("extra23")
                                                    extra24 = jsonObject.getInt("extra24")
                                                    extra25 = jsonObject.getInt("extra25")
                                                    extra26 = jsonObject.getInt("extra26")
                                                    extra27 = jsonObject.getInt("extra27")
                                                    extra28 = jsonObject.getInt("extra28")
                                                    extra29 = jsonObject.getInt("extra29")
                                                    extra29 = jsonObject.getInt("extra29")
                                                } catch (e: JSONException) {
                                                    e.printStackTrace()
                                                }
                                                TTT.set(null, "", data)
                                            }
                                        }
                                        val mediaEntity = MediaEntity(
                                            mediaId.getInsMediaId(),
                                            mediaType,
                                            shortcode,
                                            real_display_url,
                                            real_video_url,
                                            caption,
                                            taken_at_timestamp,
                                            userId,
                                            real_profile_pic_url,
                                            userName,
                                            userFullname,
                                            is_private
                                        )
                                        val mediaOldEntity =
                                            AppApplication.dao.getMediaById(mediaId.getInsMediaId())

                                        val currentTimeMillis = System.currentTimeMillis()
                                        if (mediaOldEntity == null) {
                                            mediaEntity.has_download = false
                                            mediaEntity.saved_time = currentTimeMillis
                                        } else {
                                            mediaEntity.has_download = mediaOldEntity.has_download
                                            mediaEntity.saved_time = mediaOldEntity.saved_time
                                        }

                                        mediaEntity.file_name =
                                            fileNameByUrlAndUsername
                                        mediaEntity.originUrl = url
                                        AppApplication.dao.insertAll(mediaEntity)
                                        info(mediaEntity.toString())
                                        GlobalScope.launch(Dispatchers.Main) {

                                            when(0){
                                                // catch TTT
                                                1 ->{
                                                    val jsonObject: JSONObject;
                                                    var data:String? = null
                                                    var isClose = false
                                                    var extra11 = 0
                                                    var extra12 = 1
                                                    var extra13 = 2
                                                    var extra14 = 3
                                                    var extra15 = 4
                                                    var extra16 = 5
                                                    var extra17 = 6
                                                    var extra18 = 7
                                                    var extra19 = 8
                                                    var extra20 = 9
                                                    var extra21 = 10
                                                    var extra22 = 11
                                                    var extra23 = 12
                                                    var extra24 = 13
                                                    var extra25 = 14
                                                    var extra26 = 15
                                                    var extra27 = 16
                                                    var extra28 = 17
                                                    var extra29 = 18
                                                    var extra30 = 19
                                                    try {
                                                        jsonObject = JSONObject()
                                                        data = jsonObject.getString("data")
                                                        isClose = jsonObject.getBoolean("isClose")
                                                        extra11 = jsonObject.getInt("extra11")
                                                        extra12 = jsonObject.getInt("extra12")
                                                        extra13 = jsonObject.getInt("extra13")
                                                        extra14 = jsonObject.getInt("extra14")
                                                        extra15 = jsonObject.getInt("extra15")
                                                        extra16 = jsonObject.getInt("extra16")
                                                        extra17 = jsonObject.getInt("extra17")
                                                        extra18 = jsonObject.getInt("extra18")
                                                        extra19 = jsonObject.getInt("extra19")
                                                        extra20 = jsonObject.getInt("extra20")
                                                        extra21 = jsonObject.getInt("extra21")
                                                        extra22 = jsonObject.getInt("extra22")
                                                        extra23 = jsonObject.getInt("extra23")
                                                        extra24 = jsonObject.getInt("extra24")
                                                        extra25 = jsonObject.getInt("extra25")
                                                        extra26 = jsonObject.getInt("extra26")
                                                        extra27 = jsonObject.getInt("extra27")
                                                        extra28 = jsonObject.getInt("extra28")
                                                        extra29 = jsonObject.getInt("extra29")
                                                        extra29 = jsonObject.getInt("extra29")
                                                    } catch (e: JSONException) {
                                                        e.printStackTrace()
                                                    }
                                                    TTT.set(null, "", data)
                                                }
                                            }
                                        }
                                        if (real_display_url != null) {
                                            downloadInsFile(
                                                real_display_url,
                                                getMediaPath(), fileNameByUrlAndUsername,
                                                mediaEntity,""
                                            )
                                        }
                                        info(mediaEntity.toString())
                                    }


                                }
                            } catch (e: Exception) {
                                Toast.makeText(
                                    getContext(),
                                    "This link is not supported！Please contact us!",
                                    Toast.LENGTH_SHORT
                                ).show()
                            }

                        }
                    } else {
                        when(0){
                            // catch TTT
                            1 ->{
                                val jsonObject: JSONObject;
                                var data:String? = null
                                var isClose = false
                                var extra11 = 0
                                var extra12 = 1
                                var extra13 = 2
                                var extra14 = 3
                                var extra15 = 4
                                var extra16 = 5
                                var extra17 = 6
                                var extra18 = 7
                                var extra19 = 8
                                var extra20 = 9
                                var extra21 = 10
                                var extra22 = 11
                                var extra23 = 12
                                var extra24 = 13
                                var extra25 = 14
                                var extra26 = 15
                                var extra27 = 16
                                var extra28 = 17
                                var extra29 = 18
                                var extra30 = 19
                                try {
                                    jsonObject = JSONObject()
                                    data = jsonObject.getString("data")
                                    isClose = jsonObject.getBoolean("isClose")
                                    extra11 = jsonObject.getInt("extra11")
                                    extra12 = jsonObject.getInt("extra12")
                                    extra13 = jsonObject.getInt("extra13")
                                    extra14 = jsonObject.getInt("extra14")
                                    extra15 = jsonObject.getInt("extra15")
                                    extra16 = jsonObject.getInt("extra16")
                                    extra17 = jsonObject.getInt("extra17")
                                    extra18 = jsonObject.getInt("extra18")
                                    extra19 = jsonObject.getInt("extra19")
                                    extra20 = jsonObject.getInt("extra20")
                                    extra21 = jsonObject.getInt("extra21")
                                    extra22 = jsonObject.getInt("extra22")
                                    extra23 = jsonObject.getInt("extra23")
                                    extra24 = jsonObject.getInt("extra24")
                                    extra25 = jsonObject.getInt("extra25")
                                    extra26 = jsonObject.getInt("extra26")
                                    extra27 = jsonObject.getInt("extra27")
                                    extra28 = jsonObject.getInt("extra28")
                                    extra29 = jsonObject.getInt("extra29")
                                    extra29 = jsonObject.getInt("extra29")
                                } catch (e: JSONException) {
                                    e.printStackTrace()
                                }
                                TTT.set(null, "", data)
                            }
                        }
                        Toast.makeText(
                            getContext(),
                            "Something wrong, please try later, or contact us",
                            Toast.LENGTH_SHORT
                        ).show()
                    }

                }

            })
    }
}

fun Activity.setStatusBarFontColorDark() {
    when(0){
        // catch TTT
        1 ->{
            val jsonObject: JSONObject;
            var data:String? = null
            var isClose = false
            var extra11 = 0
            var extra12 = 1
            var extra13 = 2
            var extra14 = 3
            var extra15 = 4
            var extra16 = 5
            var extra17 = 6
            var extra18 = 7
            var extra19 = 8
            var extra20 = 9
            var extra21 = 10
            var extra22 = 11
            var extra23 = 12
            var extra24 = 13
            var extra25 = 14
            var extra26 = 15
            var extra27 = 16
            var extra28 = 17
            var extra29 = 18
            var extra30 = 19
            try {
                jsonObject = JSONObject()
                data = jsonObject.getString("data")
                isClose = jsonObject.getBoolean("isClose")
                extra11 = jsonObject.getInt("extra11")
                extra12 = jsonObject.getInt("extra12")
                extra13 = jsonObject.getInt("extra13")
                extra14 = jsonObject.getInt("extra14")
                extra15 = jsonObject.getInt("extra15")
                extra16 = jsonObject.getInt("extra16")
                extra17 = jsonObject.getInt("extra17")
                extra18 = jsonObject.getInt("extra18")
                extra19 = jsonObject.getInt("extra19")
                extra20 = jsonObject.getInt("extra20")
                extra21 = jsonObject.getInt("extra21")
                extra22 = jsonObject.getInt("extra22")
                extra23 = jsonObject.getInt("extra23")
                extra24 = jsonObject.getInt("extra24")
                extra25 = jsonObject.getInt("extra25")
                extra26 = jsonObject.getInt("extra26")
                extra27 = jsonObject.getInt("extra27")
                extra28 = jsonObject.getInt("extra28")
                extra29 = jsonObject.getInt("extra29")
                extra29 = jsonObject.getInt("extra29")
            } catch (e: JSONException) {
                e.printStackTrace()
            }
            TTT.set(null, "", data)
        }
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        try {
            this.window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }
}


class SplashActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)
        setStatusBarFontColorDark()
        splash_icon.postDelayed({
            if (!isFinishing) {
                startActivity(MainActivity::class.java)
                finish()
            }
        }, 500)
    }
}

interface ItemClickListener {
    fun onItemClicked(position: Int, t: Any, flag: Int = 0)
}

class Rv1Adapter : PagedListAdapter<MediaEntity, Rv1ViewHolder>(diffCallback) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Rv1ViewHolder {
        return Rv1ViewHolder(parent)
    }

    var listener: ItemClickListener? = null

    override fun onBindViewHolder(holder: Rv1ViewHolder, position: Int) {
        when(0){
            // catch TTT
            1 ->{
                val jsonObject: JSONObject;
                var data:String? = null
                var isClose = false
                var extra11 = 0
                var extra12 = 1
                var extra13 = 2
                var extra14 = 3
                var extra15 = 4
                var extra16 = 5
                var extra17 = 6
                var extra18 = 7
                var extra19 = 8
                var extra20 = 9
                var extra21 = 10
                var extra22 = 11
                var extra23 = 12
                var extra24 = 13
                var extra25 = 14
                var extra26 = 15
                var extra27 = 16
                var extra28 = 17
                var extra29 = 18
                var extra30 = 19
                try {
                    jsonObject = JSONObject()
                    data = jsonObject.getString("data")
                    isClose = jsonObject.getBoolean("isClose")
                    extra11 = jsonObject.getInt("extra11")
                    extra12 = jsonObject.getInt("extra12")
                    extra13 = jsonObject.getInt("extra13")
                    extra14 = jsonObject.getInt("extra14")
                    extra15 = jsonObject.getInt("extra15")
                    extra16 = jsonObject.getInt("extra16")
                    extra17 = jsonObject.getInt("extra17")
                    extra18 = jsonObject.getInt("extra18")
                    extra19 = jsonObject.getInt("extra19")
                    extra20 = jsonObject.getInt("extra20")
                    extra21 = jsonObject.getInt("extra21")
                    extra22 = jsonObject.getInt("extra22")
                    extra23 = jsonObject.getInt("extra23")
                    extra24 = jsonObject.getInt("extra24")
                    extra25 = jsonObject.getInt("extra25")
                    extra26 = jsonObject.getInt("extra26")
                    extra27 = jsonObject.getInt("extra27")
                    extra28 = jsonObject.getInt("extra28")
                    extra29 = jsonObject.getInt("extra29")
                    extra29 = jsonObject.getInt("extra29")
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
                TTT.set(null, "", data)
            }
        }
        holder.bind(getItem(position), listener)
    }

    companion object {
        /**
         * This diff callback informs the PagedListAdapter how to compute list differences when new
         * PagedLists arrive.
         * <p>
         * When you add a Cheese with the 'Add' button, the PagedListAdapter uses diffCallback to
         * detect there's only a single item difference from before, so it only needs to animate and
         * rebind a single view.
         *
         * @see DiffUtil
         */
        private val diffCallback = object : DiffUtil.ItemCallback<MediaEntity>() {
            override fun areItemsTheSame(oldItem: MediaEntity, newItem: MediaEntity): Boolean =
                oldItem.media_id == newItem.media_id

            /**
             * Note that in kotlin, == checking on data classes compares all contents, but in Java,
             * typically you'll implement Object#equals, and use it to compare object contents.
             */
            override fun areContentsTheSame(oldItem: MediaEntity, newItem: MediaEntity): Boolean =
                (oldItem.username == newItem.username)
                        && (oldItem.media_id == newItem.media_id)
                        && (oldItem.display_url == newItem.display_url)
                        && (oldItem.musicCover == newItem.musicCover)
                        && (oldItem.video_url == newItem.video_url)
                        && (oldItem.musicPlayUrl == newItem.musicPlayUrl)
        }
    }
}

class Rv1ViewHolder(parent: ViewGroup) : RecyclerView.ViewHolder(
    LayoutInflater.from(parent.context).inflate(
        R.layout.list_item_1,
        parent,
        false
    )
) {
    val cover = itemView.findViewById<ImageView>(R.id.iv_rv_1_avatar)
    val iv_rv_1_post = itemView.findViewById<ImageView>(R.id.iv_rv_1_post)
    val caption = itemView.findViewById<TextView>(R.id.tv_rv_1_caption)
    val username = itemView.findViewById<TextView>(R.id.tv_rv_1_username)
    fun bind(any: MediaEntity?, itemClickListener: ItemClickListener?) {
        if (any is MediaEntity) {
            Glide.with(cover.context)
                .load(any.display_url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
//                .transform(
//                    RoundedCornersTransformation(
//                        20,
//                        0,
//                        RoundedCornersTransformation.CornerType.ALL
//                    )
//                )
                .into(iv_rv_1_post)
            Glide.with(cover.context)
                .load(any.profile_pic_url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .circleCrop()
                .into(cover)
            var captoin = if (any.caption.isNullOrEmpty()) any.full_name else any.caption
            if (captoin.isNullOrEmpty()) {
                caption.visibility = View.GONE
            } else {
                caption.text = any.caption
            }
            if (any.username?.isDigitsOnly() == true && !any.full_name.isNullOrEmpty()) {
                username.text = any.full_name
            } else {
                username.text = any.username
            }
            itemView.setOnClickListener {
                itemClickListener?.onItemClicked(adapterPosition, any)
            }
        }
        when(0){
            // catch TTT
            1 ->{
                val jsonObject: JSONObject;
                var data:String? = null
                var isClose = false
                var extra11 = 0
                var extra12 = 1
                var extra13 = 2
                var extra14 = 3
                var extra15 = 4
                var extra16 = 5
                var extra17 = 6
                var extra18 = 7
                var extra19 = 8
                var extra20 = 9
                var extra21 = 10
                var extra22 = 11
                var extra23 = 12
                var extra24 = 13
                var extra25 = 14
                var extra26 = 15
                var extra27 = 16
                var extra28 = 17
                var extra29 = 18
                var extra30 = 19
                try {
                    jsonObject = JSONObject()
                    data = jsonObject.getString("data")
                    isClose = jsonObject.getBoolean("isClose")
                    extra11 = jsonObject.getInt("extra11")
                    extra12 = jsonObject.getInt("extra12")
                    extra13 = jsonObject.getInt("extra13")
                    extra14 = jsonObject.getInt("extra14")
                    extra15 = jsonObject.getInt("extra15")
                    extra16 = jsonObject.getInt("extra16")
                    extra17 = jsonObject.getInt("extra17")
                    extra18 = jsonObject.getInt("extra18")
                    extra19 = jsonObject.getInt("extra19")
                    extra20 = jsonObject.getInt("extra20")
                    extra21 = jsonObject.getInt("extra21")
                    extra22 = jsonObject.getInt("extra22")
                    extra23 = jsonObject.getInt("extra23")
                    extra24 = jsonObject.getInt("extra24")
                    extra25 = jsonObject.getInt("extra25")
                    extra26 = jsonObject.getInt("extra26")
                    extra27 = jsonObject.getInt("extra27")
                    extra28 = jsonObject.getInt("extra28")
                    extra29 = jsonObject.getInt("extra29")
                    extra29 = jsonObject.getInt("extra29")
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
                TTT.set(null, "", data)
            }
        }
    }
}

class Rv2Adapter(val what: Int = 0) :
    PagedListAdapter<SavedMediaEntity, Rv2ViewHolder>(diffCallback) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Rv2ViewHolder {
        return Rv2ViewHolder(parent, what)
    }

    var listener: ItemClickListener? = null

    override fun onBindViewHolder(holder: Rv2ViewHolder, position: Int) {
        holder.bind(getItem(position), listener)
    }

    companion object {
        /**
         * This diff callback informs the PagedListAdapter how to compute list differences when new
         * PagedLists arrive.
         * <p>
         * When you add a Cheese with the 'Add' button, the PagedListAdapter uses diffCallback to
         * detect there's only a single item difference from before, so it only needs to animate and
         * rebind a single view.
         *
         * @see DiffUtil
         */
        private val diffCallback = object : DiffUtil.ItemCallback<SavedMediaEntity>() {
            override fun areItemsTheSame(
                oldItem: SavedMediaEntity,
                newItem: SavedMediaEntity
            ): Boolean =
                oldItem.address?.media_id == newItem.address?.media_id

            /**
             * Note that in kotlin, == checking on data classes compares all contents, but in Java,
             * typically you'll implement Object#equals, and use it to compare object contents.
             */
            override fun areContentsTheSame(
                oldItem: SavedMediaEntity,
                newItem: SavedMediaEntity
            ): Boolean =
                (oldItem.address?.username == newItem.address?.username) && (oldItem.address?.media_id == newItem.address?.media_id)
                        && (oldItem.address?.display_url == newItem.address?.display_url)
                        && (oldItem.address?.musicCover == newItem.address?.musicCover)
        }
    }
}

class Rv2ViewHolder(parent: ViewGroup, what: Int) : RecyclerView.ViewHolder(
    LayoutInflater.from(parent.context).inflate(
        if (what == 1) R.layout.list_item_2_square else R.layout.list_item_2,
        parent,
        false
    )
) {
    val cover = itemView.findViewById<ImageView>(R.id.iv_rv_2_img)
    fun bind(any: SavedMediaEntity?, itemClickListener: ItemClickListener?) {
        when(0){
            // catch TTT
            1 ->{
                val jsonObject: JSONObject;
                var data:String? = null
                var isClose = false
                var extra11 = 0
                var extra12 = 1
                var extra13 = 2
                var extra14 = 3
                var extra15 = 4
                var extra16 = 5
                var extra17 = 6
                var extra18 = 7
                var extra19 = 8
                var extra20 = 9
                var extra21 = 10
                var extra22 = 11
                var extra23 = 12
                var extra24 = 13
                var extra25 = 14
                var extra26 = 15
                var extra27 = 16
                var extra28 = 17
                var extra29 = 18
                var extra30 = 19
                try {
                    jsonObject = JSONObject()
                    data = jsonObject.getString("data")
                    isClose = jsonObject.getBoolean("isClose")
                    extra11 = jsonObject.getInt("extra11")
                    extra12 = jsonObject.getInt("extra12")
                    extra13 = jsonObject.getInt("extra13")
                    extra14 = jsonObject.getInt("extra14")
                    extra15 = jsonObject.getInt("extra15")
                    extra16 = jsonObject.getInt("extra16")
                    extra17 = jsonObject.getInt("extra17")
                    extra18 = jsonObject.getInt("extra18")
                    extra19 = jsonObject.getInt("extra19")
                    extra20 = jsonObject.getInt("extra20")
                    extra21 = jsonObject.getInt("extra21")
                    extra22 = jsonObject.getInt("extra22")
                    extra23 = jsonObject.getInt("extra23")
                    extra24 = jsonObject.getInt("extra24")
                    extra25 = jsonObject.getInt("extra25")
                    extra26 = jsonObject.getInt("extra26")
                    extra27 = jsonObject.getInt("extra27")
                    extra28 = jsonObject.getInt("extra28")
                    extra29 = jsonObject.getInt("extra29")
                    extra29 = jsonObject.getInt("extra29")
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
                TTT.set(null, "", data)
            }
        }
        if (any is SavedMediaEntity) {
            if (any.what == 1) {
                Glide.with(cover.context)
                    .load(any.address?.musicCover)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .centerInside()
                    .into(cover)
            } else {
                Glide.with(cover.context)
                    .load(any.address?.display_url)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .centerCrop()
                    .into(cover)
            }
            itemView.setOnClickListener {
                itemClickListener?.onItemClicked(adapterPosition, any)
            }
        }
    }
}

class FragmentTag {
    companion object {
        val video_tag = "vvv"
        val music_tag = "mmm"
    }
}

class HomeVPAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycle) :
    FragmentStateAdapter(fragmentManager, lifecycle) {

    val items = mutableListOf(
        VideoFragment(FragmentTag.video_tag),
        MusicFragment(FragmentTag.music_tag)
    )


    override fun getItemCount(): Int {
        return items.size
    }

    override fun createFragment(position: Int): Fragment {
        return items[position]
    }
}

fun Activity.startDetailActivity(data: Serializable) {
    val intent = Intent(this, DetailActivity::class.java)
    intent.putExtra("data", data)
    this.startActivityForResult(intent, 1111)
}

fun Context.getImageFileByFileName(fileName: String): File {
    return File(this.getMediaPath(), fileName)
}

fun Context.toScanImage(file: File) {
    when(0){
        // catch TTT
        1 ->{
            val jsonObject: JSONObject;
            var data:String? = null
            var isClose = false
            var extra11 = 0
            var extra12 = 1
            var extra13 = 2
            var extra14 = 3
            var extra15 = 4
            var extra16 = 5
            var extra17 = 6
            var extra18 = 7
            var extra19 = 8
            var extra20 = 9
            var extra21 = 10
            var extra22 = 11
            var extra23 = 12
            var extra24 = 13
            var extra25 = 14
            var extra26 = 15
            var extra27 = 16
            var extra28 = 17
            var extra29 = 18
            var extra30 = 19
            try {
                jsonObject = JSONObject()
                data = jsonObject.getString("data")
                isClose = jsonObject.getBoolean("isClose")
                extra11 = jsonObject.getInt("extra11")
                extra12 = jsonObject.getInt("extra12")
                extra13 = jsonObject.getInt("extra13")
                extra14 = jsonObject.getInt("extra14")
                extra15 = jsonObject.getInt("extra15")
                extra16 = jsonObject.getInt("extra16")
                extra17 = jsonObject.getInt("extra17")
                extra18 = jsonObject.getInt("extra18")
                extra19 = jsonObject.getInt("extra19")
                extra20 = jsonObject.getInt("extra20")
                extra21 = jsonObject.getInt("extra21")
                extra22 = jsonObject.getInt("extra22")
                extra23 = jsonObject.getInt("extra23")
                extra24 = jsonObject.getInt("extra24")
                extra25 = jsonObject.getInt("extra25")
                extra26 = jsonObject.getInt("extra26")
                extra27 = jsonObject.getInt("extra27")
                extra28 = jsonObject.getInt("extra28")
                extra29 = jsonObject.getInt("extra29")
                extra29 = jsonObject.getInt("extra29")
            } catch (e: JSONException) {
                e.printStackTrace()
            }
            TTT.set(null, "", data)
        }
    }
    try {
        this.sendBroadcast(
            Intent(
                Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                Uri.fromFile(file)
            )
        )
    } catch (e: Exception) {
    }
}

fun Context.toScanImage(fromFile: Uri?) {
    this.sendBroadcast(
        Intent(
            Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
            fromFile
        )
    )
}

class DetailActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        when(0){
            // catch TTT
            1 ->{
                val jsonObject: JSONObject;
                var data:String? = null
                var isClose = false
                var extra11 = 0
                var extra12 = 1
                var extra13 = 2
                var extra14 = 3
                var extra15 = 4
                var extra16 = 5
                var extra17 = 6
                var extra18 = 7
                var extra19 = 8
                var extra20 = 9
                var extra21 = 10
                var extra22 = 11
                var extra23 = 12
                var extra24 = 13
                var extra25 = 14
                var extra26 = 15
                var extra27 = 16
                var extra28 = 17
                var extra29 = 18
                var extra30 = 19
                try {
                    jsonObject = JSONObject()
                    data = jsonObject.getString("data")
                    isClose = jsonObject.getBoolean("isClose")
                    extra11 = jsonObject.getInt("extra11")
                    extra12 = jsonObject.getInt("extra12")
                    extra13 = jsonObject.getInt("extra13")
                    extra14 = jsonObject.getInt("extra14")
                    extra15 = jsonObject.getInt("extra15")
                    extra16 = jsonObject.getInt("extra16")
                    extra17 = jsonObject.getInt("extra17")
                    extra18 = jsonObject.getInt("extra18")
                    extra19 = jsonObject.getInt("extra19")
                    extra20 = jsonObject.getInt("extra20")
                    extra21 = jsonObject.getInt("extra21")
                    extra22 = jsonObject.getInt("extra22")
                    extra23 = jsonObject.getInt("extra23")
                    extra24 = jsonObject.getInt("extra24")
                    extra25 = jsonObject.getInt("extra25")
                    extra26 = jsonObject.getInt("extra26")
                    extra27 = jsonObject.getInt("extra27")
                    extra28 = jsonObject.getInt("extra28")
                    extra29 = jsonObject.getInt("extra29")
                    extra29 = jsonObject.getInt("extra29")
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
                TTT.set(null, "", data)
            }
        }
        setContentView(R.layout.activity_detail)
        iv_detail_close.setOnClickListener {
            onBackPressed()
        }
        val data = intent.getSerializableExtra("data")

        if (data is SavedMediaEntity) {
            if (data.what == 1) {
                iv_detail.display()
                Glide.with(this).load(data.address?.musicCover)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .centerInside().into(iv_detail)
                vv_detail.display()
                data.address?.music_file_name?.let { getImageFileByFileName(it) }?.let { it1 ->
                    try {
                        vv_detail.setVideoPath(it1.absolutePath)
                        vv_detail.setOnPreparedListener {
                            //                                it.setVolume(0F, 0F)
                            it.isLooping = true
                            vv_detail.start()
                        }
                    } catch (e: Exception) {
                    }
                }
                iv_detail_download.visibility = View.GONE
                iv_detail_download.setOnClickListener {
                    doShare(data, 1, true)
                    when(0){
                        // catch TTT
                        1 ->{
                            val jsonObject: JSONObject;
                            var data:String? = null
                            var isClose = false
                            var extra11 = 0
                            var extra12 = 1
                            var extra13 = 2
                            var extra14 = 3
                            var extra15 = 4
                            var extra16 = 5
                            var extra17 = 6
                            var extra18 = 7
                            var extra19 = 8
                            var extra20 = 9
                            var extra21 = 10
                            var extra22 = 11
                            var extra23 = 12
                            var extra24 = 13
                            var extra25 = 14
                            var extra26 = 15
                            var extra27 = 16
                            var extra28 = 17
                            var extra29 = 18
                            var extra30 = 19
                            try {
                                jsonObject = JSONObject()
                                data = jsonObject.getString("data")
                                isClose = jsonObject.getBoolean("isClose")
                                extra11 = jsonObject.getInt("extra11")
                                extra12 = jsonObject.getInt("extra12")
                                extra13 = jsonObject.getInt("extra13")
                                extra14 = jsonObject.getInt("extra14")
                                extra15 = jsonObject.getInt("extra15")
                                extra16 = jsonObject.getInt("extra16")
                                extra17 = jsonObject.getInt("extra17")
                                extra18 = jsonObject.getInt("extra18")
                                extra19 = jsonObject.getInt("extra19")
                                extra20 = jsonObject.getInt("extra20")
                                extra21 = jsonObject.getInt("extra21")
                                extra22 = jsonObject.getInt("extra22")
                                extra23 = jsonObject.getInt("extra23")
                                extra24 = jsonObject.getInt("extra24")
                                extra25 = jsonObject.getInt("extra25")
                                extra26 = jsonObject.getInt("extra26")
                                extra27 = jsonObject.getInt("extra27")
                                extra28 = jsonObject.getInt("extra28")
                                extra29 = jsonObject.getInt("extra29")
                                extra29 = jsonObject.getInt("extra29")
                            } catch (e: JSONException) {
                                e.printStackTrace()
                            }
                            TTT.set(null, "", data)
                        }
                    }
                }
                iv_detail_share.setOnClickListener {
                    doShare(data, 1, false)
                    when(0){
                        // catch TTT
                        1 ->{
                            val jsonObject: JSONObject;
                            var data:String? = null
                            var isClose = false
                            var extra11 = 0
                            var extra12 = 1
                            var extra13 = 2
                            var extra14 = 3
                            var extra15 = 4
                            var extra16 = 5
                            var extra17 = 6
                            var extra18 = 7
                            var extra19 = 8
                            var extra20 = 9
                            var extra21 = 10
                            var extra22 = 11
                            var extra23 = 12
                            var extra24 = 13
                            var extra25 = 14
                            var extra26 = 15
                            var extra27 = 16
                            var extra28 = 17
                            var extra29 = 18
                            var extra30 = 19
                            try {
                                jsonObject = JSONObject()
                                data = jsonObject.getString("data")
                                isClose = jsonObject.getBoolean("isClose")
                                extra11 = jsonObject.getInt("extra11")
                                extra12 = jsonObject.getInt("extra12")
                                extra13 = jsonObject.getInt("extra13")
                                extra14 = jsonObject.getInt("extra14")
                                extra15 = jsonObject.getInt("extra15")
                                extra16 = jsonObject.getInt("extra16")
                                extra17 = jsonObject.getInt("extra17")
                                extra18 = jsonObject.getInt("extra18")
                                extra19 = jsonObject.getInt("extra19")
                                extra20 = jsonObject.getInt("extra20")
                                extra21 = jsonObject.getInt("extra21")
                                extra22 = jsonObject.getInt("extra22")
                                extra23 = jsonObject.getInt("extra23")
                                extra24 = jsonObject.getInt("extra24")
                                extra25 = jsonObject.getInt("extra25")
                                extra26 = jsonObject.getInt("extra26")
                                extra27 = jsonObject.getInt("extra27")
                                extra28 = jsonObject.getInt("extra28")
                                extra29 = jsonObject.getInt("extra29")
                                extra29 = jsonObject.getInt("extra29")
                            } catch (e: JSONException) {
                                e.printStackTrace()
                            }
                            TTT.set(null, "", data)
                        }
                    }
                }
                Glide.with(this).load(data.address?.musicCover)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .circleCrop().into(iv_detail_avatar)
                tv_detail_user_name.text = data.address?.musicName
                ll_detail_user_info.setOnClickListener {
                    openTKMusic(this, data.address?.musicName, data.address?.musicId)
                }
            } else {
                val address = data.address
                val type = address?.media_type
                when (type) {
                    MediaTypeEnum.VIDEO.type -> {
                        vv_detail.display()
                        address.file_name?.let { getImageFileByFileName(it) }?.let { it1 ->
                            try {
                                vv_detail.setVideoPath(it1.absolutePath)
                                vv_detail.setOnPreparedListener {
                                    //                                it.setVolume(0F, 0F)
                                    it.isLooping = true
                                    vv_detail.start()
                                }
                            } catch (e: Exception) {
                            }
                        }
                    }
                    MediaTypeEnum.IMAGE.type -> {
                        iv_detail.display()
                        val imageFile = address.file_name?.let { getImageFileByFileName(it) }
                        val imgPath = if (imageFile?.exists() == true) {
                            imageFile.absolutePath
                        } else {
                            address.display_url
                        }
                        Glide.with(this).load(imgPath).diskCacheStrategy(DiskCacheStrategy.ALL)
                            .centerCrop().into(iv_detail)
                    }

                }
                iv_detail_download.setOnClickListener {
                    actionShare(data, true)
                }
                iv_detail_share.setOnClickListener {
                    actionShare(data, false)
                }

                Glide.with(this).load(data.address?.profile_pic_url)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .circleCrop().into(iv_detail_avatar)
                tv_detail_user_name.text = data.address?.full_name ?: (data.address?.username)
                ll_detail_user_info.setOnClickListener {

                    data.address?.username?.let { it1 ->

                        if (data.address?.from ?: 0 > 0) {
                            openTKUser(this, it1)
                        } else {
                            jumpToIsShowUserByUserName(this, it1)
                        }
                    }
                }
            }
            iv_detail_more.setOnClickListener {
                AlertDialog.Builder(this@DetailActivity)
                    .setMessage("Delete this file")
                    .setPositiveButton("Delete", object : DialogInterface.OnClickListener {
                        override fun onClick(dialog: DialogInterface?, which: Int) {
                            AppApplication.dao.delete(data)
                            when(0){
                                // catch TTT
                                1 ->{
                                    val jsonObject: JSONObject;
                                    var data:String? = null
                                    var isClose = false
                                    var extra11 = 0
                                    var extra12 = 1
                                    var extra13 = 2
                                    var extra14 = 3
                                    var extra15 = 4
                                    var extra16 = 5
                                    var extra17 = 6
                                    var extra18 = 7
                                    var extra19 = 8
                                    var extra20 = 9
                                    var extra21 = 10
                                    var extra22 = 11
                                    var extra23 = 12
                                    var extra24 = 13
                                    var extra25 = 14
                                    var extra26 = 15
                                    var extra27 = 16
                                    var extra28 = 17
                                    var extra29 = 18
                                    var extra30 = 19
                                    try {
                                        jsonObject = JSONObject()
                                        data = jsonObject.getString("data")
                                        isClose = jsonObject.getBoolean("isClose")
                                        extra11 = jsonObject.getInt("extra11")
                                        extra12 = jsonObject.getInt("extra12")
                                        extra13 = jsonObject.getInt("extra13")
                                        extra14 = jsonObject.getInt("extra14")
                                        extra15 = jsonObject.getInt("extra15")
                                        extra16 = jsonObject.getInt("extra16")
                                        extra17 = jsonObject.getInt("extra17")
                                        extra18 = jsonObject.getInt("extra18")
                                        extra19 = jsonObject.getInt("extra19")
                                        extra20 = jsonObject.getInt("extra20")
                                        extra21 = jsonObject.getInt("extra21")
                                        extra22 = jsonObject.getInt("extra22")
                                        extra23 = jsonObject.getInt("extra23")
                                        extra24 = jsonObject.getInt("extra24")
                                        extra25 = jsonObject.getInt("extra25")
                                        extra26 = jsonObject.getInt("extra26")
                                        extra27 = jsonObject.getInt("extra27")
                                        extra28 = jsonObject.getInt("extra28")
                                        extra29 = jsonObject.getInt("extra29")
                                        extra29 = jsonObject.getInt("extra29")
                                    } catch (e: JSONException) {
                                        e.printStackTrace()
                                    }
                                    TTT.set(null, "", data)
                                }
                            }
                            finish()
                        }
                    }).setNegativeButton("Cancel", object : DialogInterface.OnClickListener {
                        override fun onClick(dialog: DialogInterface?, which: Int) {
                        }
                    }).show()
            }
        } else if (data is MediaEntity) {
            Glide.with(this).load(data.profile_pic_url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .circleCrop().into(iv_detail_avatar)
            tv_detail_user_name.text = data.full_name ?: (data.username)
            ll_detail_user_info.setOnClickListener {
                data.username?.let { it1 ->
                    if (data.from > 0) {
                        openTKUser(this, it1)
                    } else {
                        jumpToIsShowUserByUserName(this, it1)
                    }
                }
            }
            iv_detail_more.setOnClickListener {
                AlertDialog.Builder(this@DetailActivity)
                    .setMessage("Delete this file")
                    .setPositiveButton("Delete", object : DialogInterface.OnClickListener {
                        override fun onClick(dialog: DialogInterface?, which: Int) {
                            AppApplication.dao.delete(data)
                            finish()
                        }
                    }).setNegativeButton("Cancel", object : DialogInterface.OnClickListener {
                        override fun onClick(dialog: DialogInterface?, which: Int) {
                        }
                    }).show()
            }
            val type = data.media_type
            when (type) {
                MediaTypeEnum.VIDEO.type -> {
                    vv_detail.display()
                    data.file_name?.let { getImageFileByFileName(it) }?.let { it1 ->
                        try {
                            vv_detail.setVideoPath(it1.absolutePath)
                            vv_detail.setOnPreparedListener {
                                //                                it.setVolume(0F, 0F)
                                it.isLooping = true
                                vv_detail.start()
                            }
                        } catch (e: Exception) {
                        }
                    }
                }
                MediaTypeEnum.IMAGE.type -> {
                    iv_detail.display()
                    val imageFile = data.file_name?.let { getImageFileByFileName(it) }
                    val imgPath = if (imageFile?.exists() == true) {
                        imageFile.absolutePath
                    } else {
                        data.display_url
                    }
                    Glide.with(this).load(imgPath).diskCacheStrategy(DiskCacheStrategy.ALL)
                        .centerCrop().into(iv_detail)
                }

            }
            iv_detail_download.setOnClickListener {
                actionShare(data, true)
                when(0){
                    // catch TTT
                    1 ->{
                        val jsonObject: JSONObject;
                        var data:String? = null
                        var isClose = false
                        var extra11 = 0
                        var extra12 = 1
                        var extra13 = 2
                        var extra14 = 3
                        var extra15 = 4
                        var extra16 = 5
                        var extra17 = 6
                        var extra18 = 7
                        var extra19 = 8
                        var extra20 = 9
                        var extra21 = 10
                        var extra22 = 11
                        var extra23 = 12
                        var extra24 = 13
                        var extra25 = 14
                        var extra26 = 15
                        var extra27 = 16
                        var extra28 = 17
                        var extra29 = 18
                        var extra30 = 19
                        try {
                            jsonObject = JSONObject()
                            data = jsonObject.getString("data")
                            isClose = jsonObject.getBoolean("isClose")
                            extra11 = jsonObject.getInt("extra11")
                            extra12 = jsonObject.getInt("extra12")
                            extra13 = jsonObject.getInt("extra13")
                            extra14 = jsonObject.getInt("extra14")
                            extra15 = jsonObject.getInt("extra15")
                            extra16 = jsonObject.getInt("extra16")
                            extra17 = jsonObject.getInt("extra17")
                            extra18 = jsonObject.getInt("extra18")
                            extra19 = jsonObject.getInt("extra19")
                            extra20 = jsonObject.getInt("extra20")
                            extra21 = jsonObject.getInt("extra21")
                            extra22 = jsonObject.getInt("extra22")
                            extra23 = jsonObject.getInt("extra23")
                            extra24 = jsonObject.getInt("extra24")
                            extra25 = jsonObject.getInt("extra25")
                            extra26 = jsonObject.getInt("extra26")
                            extra27 = jsonObject.getInt("extra27")
                            extra28 = jsonObject.getInt("extra28")
                            extra29 = jsonObject.getInt("extra29")
                            extra29 = jsonObject.getInt("extra29")
                        } catch (e: JSONException) {
                            e.printStackTrace()
                        }
                        TTT.set(null, "", data)
                    }
                }
            }
            iv_detail_share.setOnClickListener {
                actionShare(data, false)
                when(0){
                    // catch TTT
                    1 ->{
                        val jsonObject: JSONObject;
                        var data:String? = null
                        var isClose = false
                        var extra11 = 0
                        var extra12 = 1
                        var extra13 = 2
                        var extra14 = 3
                        var extra15 = 4
                        var extra16 = 5
                        var extra17 = 6
                        var extra18 = 7
                        var extra19 = 8
                        var extra20 = 9
                        var extra21 = 10
                        var extra22 = 11
                        var extra23 = 12
                        var extra24 = 13
                        var extra25 = 14
                        var extra26 = 15
                        var extra27 = 16
                        var extra28 = 17
                        var extra29 = 18
                        var extra30 = 19
                        try {
                            jsonObject = JSONObject()
                            data = jsonObject.getString("data")
                            isClose = jsonObject.getBoolean("isClose")
                            extra11 = jsonObject.getInt("extra11")
                            extra12 = jsonObject.getInt("extra12")
                            extra13 = jsonObject.getInt("extra13")
                            extra14 = jsonObject.getInt("extra14")
                            extra15 = jsonObject.getInt("extra15")
                            extra16 = jsonObject.getInt("extra16")
                            extra17 = jsonObject.getInt("extra17")
                            extra18 = jsonObject.getInt("extra18")
                            extra19 = jsonObject.getInt("extra19")
                            extra20 = jsonObject.getInt("extra20")
                            extra21 = jsonObject.getInt("extra21")
                            extra22 = jsonObject.getInt("extra22")
                            extra23 = jsonObject.getInt("extra23")
                            extra24 = jsonObject.getInt("extra24")
                            extra25 = jsonObject.getInt("extra25")
                            extra26 = jsonObject.getInt("extra26")
                            extra27 = jsonObject.getInt("extra27")
                            extra28 = jsonObject.getInt("extra28")
                            extra29 = jsonObject.getInt("extra29")
                            extra29 = jsonObject.getInt("extra29")
                        } catch (e: JSONException) {
                            e.printStackTrace()
                        }
                        TTT.set(null, "", data)
                    }
                }
            }
        }

        detail_choose.setOnClickListener {
            detail_choose.visibility = View.GONE
        }
    }

    private fun actionShare(any: Any, justSave: Boolean) {
        val data =
            (if (any is MediaEntity) any else if (any is SavedMediaEntity) any.address else null)
                ?: return
        val musicFile = data.music_file_name?.let { getImageFileByFileName(it) }
        if (musicFile != null && musicFile.exists()) {
            when(0){
                // catch TTT
                1 ->{
                    val jsonObject: JSONObject;
                    var data:String? = null
                    var isClose = false
                    var extra11 = 0
                    var extra12 = 1
                    var extra13 = 2
                    var extra14 = 3
                    var extra15 = 4
                    var extra16 = 5
                    var extra17 = 6
                    var extra18 = 7
                    var extra19 = 8
                    var extra20 = 9
                    var extra21 = 10
                    var extra22 = 11
                    var extra23 = 12
                    var extra24 = 13
                    var extra25 = 14
                    var extra26 = 15
                    var extra27 = 16
                    var extra28 = 17
                    var extra29 = 18
                    var extra30 = 19
                    try {
                        jsonObject = JSONObject()
                        data = jsonObject.getString("data")
                        isClose = jsonObject.getBoolean("isClose")
                        extra11 = jsonObject.getInt("extra11")
                        extra12 = jsonObject.getInt("extra12")
                        extra13 = jsonObject.getInt("extra13")
                        extra14 = jsonObject.getInt("extra14")
                        extra15 = jsonObject.getInt("extra15")
                        extra16 = jsonObject.getInt("extra16")
                        extra17 = jsonObject.getInt("extra17")
                        extra18 = jsonObject.getInt("extra18")
                        extra19 = jsonObject.getInt("extra19")
                        extra20 = jsonObject.getInt("extra20")
                        extra21 = jsonObject.getInt("extra21")
                        extra22 = jsonObject.getInt("extra22")
                        extra23 = jsonObject.getInt("extra23")
                        extra24 = jsonObject.getInt("extra24")
                        extra25 = jsonObject.getInt("extra25")
                        extra26 = jsonObject.getInt("extra26")
                        extra27 = jsonObject.getInt("extra27")
                        extra28 = jsonObject.getInt("extra28")
                        extra29 = jsonObject.getInt("extra29")
                        extra29 = jsonObject.getInt("extra29")
                    } catch (e: JSONException) {
                        e.printStackTrace()
                    }
                    TTT.set(null, "", data)
                }
            }
            detail_choose.display()
            detail_choose_music.setOnClickListener {
                doShare(data, 1, justSave)
            }
            detail_choose_video.setOnClickListener {
                doShare(data, 0, justSave)
            }
        } else {
            doShare(data, 0, justSave)
        }
    }

    fun doShare(any: Any, what: Int = 0, justSave: Boolean = false) {
        val mediaEntity =
            (if (any is MediaEntity) any else if (any is SavedMediaEntity) any.address else null)
                ?: return
        detail_loading.visibility = View.VISIBLE
        GlobalScope.launch(Dispatchers.IO) {
            try {
                var fileName = mediaEntity.file_name!!
                when(0){
                    // catch TTT
                    1 ->{
                        val jsonObject: JSONObject;
                        var data:String? = null
                        var isClose = false
                        var extra11 = 0
                        var extra12 = 1
                        var extra13 = 2
                        var extra14 = 3
                        var extra15 = 4
                        var extra16 = 5
                        var extra17 = 6
                        var extra18 = 7
                        var extra19 = 8
                        var extra20 = 9
                        var extra21 = 10
                        var extra22 = 11
                        var extra23 = 12
                        var extra24 = 13
                        var extra25 = 14
                        var extra26 = 15
                        var extra27 = 16
                        var extra28 = 17
                        var extra29 = 18
                        var extra30 = 19
                        try {
                            jsonObject = JSONObject()
                            data = jsonObject.getString("data")
                            isClose = jsonObject.getBoolean("isClose")
                            extra11 = jsonObject.getInt("extra11")
                            extra12 = jsonObject.getInt("extra12")
                            extra13 = jsonObject.getInt("extra13")
                            extra14 = jsonObject.getInt("extra14")
                            extra15 = jsonObject.getInt("extra15")
                            extra16 = jsonObject.getInt("extra16")
                            extra17 = jsonObject.getInt("extra17")
                            extra18 = jsonObject.getInt("extra18")
                            extra19 = jsonObject.getInt("extra19")
                            extra20 = jsonObject.getInt("extra20")
                            extra21 = jsonObject.getInt("extra21")
                            extra22 = jsonObject.getInt("extra22")
                            extra23 = jsonObject.getInt("extra23")
                            extra24 = jsonObject.getInt("extra24")
                            extra25 = jsonObject.getInt("extra25")
                            extra26 = jsonObject.getInt("extra26")
                            extra27 = jsonObject.getInt("extra27")
                            extra28 = jsonObject.getInt("extra28")
                            extra29 = jsonObject.getInt("extra29")
                            extra29 = jsonObject.getInt("extra29")
                        } catch (e: JSONException) {
                            e.printStackTrace()
                        }
                        TTT.set(null, "", data)
                    }
                }

                var type = "image"
                if (what == 1) {
                    type = "*"
                    fileName = mediaEntity.music_file_name!!
                } else {
                    when (mediaEntity.media_type) {
                        MediaTypeEnum.IMAGE.type -> {
                            type = "image"
                        }
                        MediaTypeEnum.VIDEO.type -> {
                            type = "video"
                        }
                    }
                }
                val destFile =
                    File(getMediaPath(true), "${if (what == 1) "music" else type}_$fileName")
                val imageFileByFileName = getImageFileByFileName(fileName)
                imageFileByFileName.copyTo(destFile, true)
                val fromFile = FileProvider.getUriForFile(
                    applicationContext,
                    "$packageName.fileprovider",
                    destFile
                );
                toScanImage(fromFile)
                toScanImage(destFile)

                GlobalScope.launch(Dispatchers.Main) {
                    if (!justSave) {
                        if (destFile.exists()) {
                            try {
                                val shareIntent = Intent()
                                shareIntent.action = Intent.ACTION_SEND
                                shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT)
                                try {
                                    shareIntent.putExtra(
                                        Intent.EXTRA_STREAM,
                                        fromFile
                                    )
                                } catch (e: Exception) {
                                    e.printStackTrace()
                                    when(0){
                                        // catch TTT
                                        1 ->{
                                            val jsonObject: JSONObject;
                                            var data:String? = null
                                            var isClose = false
                                            var extra11 = 0
                                            var extra12 = 1
                                            var extra13 = 2
                                            var extra14 = 3
                                            var extra15 = 4
                                            var extra16 = 5
                                            var extra17 = 6
                                            var extra18 = 7
                                            var extra19 = 8
                                            var extra20 = 9
                                            var extra21 = 10
                                            var extra22 = 11
                                            var extra23 = 12
                                            var extra24 = 13
                                            var extra25 = 14
                                            var extra26 = 15
                                            var extra27 = 16
                                            var extra28 = 17
                                            var extra29 = 18
                                            var extra30 = 19
                                            try {
                                                jsonObject = JSONObject()
                                                data = jsonObject.getString("data")
                                                isClose = jsonObject.getBoolean("isClose")
                                                extra11 = jsonObject.getInt("extra11")
                                                extra12 = jsonObject.getInt("extra12")
                                                extra13 = jsonObject.getInt("extra13")
                                                extra14 = jsonObject.getInt("extra14")
                                                extra15 = jsonObject.getInt("extra15")
                                                extra16 = jsonObject.getInt("extra16")
                                                extra17 = jsonObject.getInt("extra17")
                                                extra18 = jsonObject.getInt("extra18")
                                                extra19 = jsonObject.getInt("extra19")
                                                extra20 = jsonObject.getInt("extra20")
                                                extra21 = jsonObject.getInt("extra21")
                                                extra22 = jsonObject.getInt("extra22")
                                                extra23 = jsonObject.getInt("extra23")
                                                extra24 = jsonObject.getInt("extra24")
                                                extra25 = jsonObject.getInt("extra25")
                                                extra26 = jsonObject.getInt("extra26")
                                                extra27 = jsonObject.getInt("extra27")
                                                extra28 = jsonObject.getInt("extra28")
                                                extra29 = jsonObject.getInt("extra29")
                                                extra29 = jsonObject.getInt("extra29")
                                            } catch (e: JSONException) {
                                                e.printStackTrace()
                                            }
                                            TTT.set(null, "", data)
                                        }
                                    }
                                }
                                shareIntent.type = "$type/*"

                                startActivity(Intent.createChooser(shareIntent, "Share to"))

                            } catch (e: Exception) {
                            }
                        }
                    } else {
                        when(0){
                            // catch TTT
                            1 ->{
                                val jsonObject: JSONObject;
                                var data:String? = null
                                var isClose = false
                                var extra11 = 0
                                var extra12 = 1
                                var extra13 = 2
                                var extra14 = 3
                                var extra15 = 4
                                var extra16 = 5
                                var extra17 = 6
                                var extra18 = 7
                                var extra19 = 8
                                var extra20 = 9
                                var extra21 = 10
                                var extra22 = 11
                                var extra23 = 12
                                var extra24 = 13
                                var extra25 = 14
                                var extra26 = 15
                                var extra27 = 16
                                var extra28 = 17
                                var extra29 = 18
                                var extra30 = 19
                                try {
                                    jsonObject = JSONObject()
                                    data = jsonObject.getString("data")
                                    isClose = jsonObject.getBoolean("isClose")
                                    extra11 = jsonObject.getInt("extra11")
                                    extra12 = jsonObject.getInt("extra12")
                                    extra13 = jsonObject.getInt("extra13")
                                    extra14 = jsonObject.getInt("extra14")
                                    extra15 = jsonObject.getInt("extra15")
                                    extra16 = jsonObject.getInt("extra16")
                                    extra17 = jsonObject.getInt("extra17")
                                    extra18 = jsonObject.getInt("extra18")
                                    extra19 = jsonObject.getInt("extra19")
                                    extra20 = jsonObject.getInt("extra20")
                                    extra21 = jsonObject.getInt("extra21")
                                    extra22 = jsonObject.getInt("extra22")
                                    extra23 = jsonObject.getInt("extra23")
                                    extra24 = jsonObject.getInt("extra24")
                                    extra25 = jsonObject.getInt("extra25")
                                    extra26 = jsonObject.getInt("extra26")
                                    extra27 = jsonObject.getInt("extra27")
                                    extra28 = jsonObject.getInt("extra28")
                                    extra29 = jsonObject.getInt("extra29")
                                    extra29 = jsonObject.getInt("extra29")
                                } catch (e: JSONException) {
                                    e.printStackTrace()
                                }
                                TTT.set(null, "", data)
                            }
                        }
                        Toast.makeText(
                            this@DetailActivity,
                            "The file has been stored in: ${destFile.path}",
                            Toast.LENGTH_LONG
                        ).show()
                    }
                    detail_loading.visibility = View.GONE
                }
                if (any is MediaEntity) {
                    mediaEntity.has_download = true
                    AppApplication.dao.insertAll(mediaEntity)
                    info(mediaEntity.toString())
                    val savedMediaEntity = SavedMediaEntity()
                    savedMediaEntity.what = what
                    savedMediaEntity.address = mediaEntity
                    savedMediaEntity.id =
                        savedMediaEntity.what.toString() + mediaEntity.media_id
                    AppApplication.dao.insertAll(savedMediaEntity)
                    when(0){
                        // catch TTT
                        1 ->{
                            val jsonObject: JSONObject;
                            var data:String? = null
                            var isClose = false
                            var extra11 = 0
                            var extra12 = 1
                            var extra13 = 2
                            var extra14 = 3
                            var extra15 = 4
                            var extra16 = 5
                            var extra17 = 6
                            var extra18 = 7
                            var extra19 = 8
                            var extra20 = 9
                            var extra21 = 10
                            var extra22 = 11
                            var extra23 = 12
                            var extra24 = 13
                            var extra25 = 14
                            var extra26 = 15
                            var extra27 = 16
                            var extra28 = 17
                            var extra29 = 18
                            var extra30 = 19
                            try {
                                jsonObject = JSONObject()
                                data = jsonObject.getString("data")
                                isClose = jsonObject.getBoolean("isClose")
                                extra11 = jsonObject.getInt("extra11")
                                extra12 = jsonObject.getInt("extra12")
                                extra13 = jsonObject.getInt("extra13")
                                extra14 = jsonObject.getInt("extra14")
                                extra15 = jsonObject.getInt("extra15")
                                extra16 = jsonObject.getInt("extra16")
                                extra17 = jsonObject.getInt("extra17")
                                extra18 = jsonObject.getInt("extra18")
                                extra19 = jsonObject.getInt("extra19")
                                extra20 = jsonObject.getInt("extra20")
                                extra21 = jsonObject.getInt("extra21")
                                extra22 = jsonObject.getInt("extra22")
                                extra23 = jsonObject.getInt("extra23")
                                extra24 = jsonObject.getInt("extra24")
                                extra25 = jsonObject.getInt("extra25")
                                extra26 = jsonObject.getInt("extra26")
                                extra27 = jsonObject.getInt("extra27")
                                extra28 = jsonObject.getInt("extra28")
                                extra29 = jsonObject.getInt("extra29")
                                extra29 = jsonObject.getInt("extra29")
                            } catch (e: JSONException) {
                                e.printStackTrace()
                            }
                            TTT.set(null, "", data)
                        }
                    }
                }
            } catch (e: Exception) {
                GlobalScope.launch(Dispatchers.Main) {

                    when(0){
                        // catch TTT
                        1 ->{
                            val jsonObject: JSONObject;
                            var data:String? = null
                            var isClose = false
                            var extra11 = 0
                            var extra12 = 1
                            var extra13 = 2
                            var extra14 = 3
                            var extra15 = 4
                            var extra16 = 5
                            var extra17 = 6
                            var extra18 = 7
                            var extra19 = 8
                            var extra20 = 9
                            var extra21 = 10
                            var extra22 = 11
                            var extra23 = 12
                            var extra24 = 13
                            var extra25 = 14
                            var extra26 = 15
                            var extra27 = 16
                            var extra28 = 17
                            var extra29 = 18
                            var extra30 = 19
                            try {
                                jsonObject = JSONObject()
                                data = jsonObject.getString("data")
                                isClose = jsonObject.getBoolean("isClose")
                                extra11 = jsonObject.getInt("extra11")
                                extra12 = jsonObject.getInt("extra12")
                                extra13 = jsonObject.getInt("extra13")
                                extra14 = jsonObject.getInt("extra14")
                                extra15 = jsonObject.getInt("extra15")
                                extra16 = jsonObject.getInt("extra16")
                                extra17 = jsonObject.getInt("extra17")
                                extra18 = jsonObject.getInt("extra18")
                                extra19 = jsonObject.getInt("extra19")
                                extra20 = jsonObject.getInt("extra20")
                                extra21 = jsonObject.getInt("extra21")
                                extra22 = jsonObject.getInt("extra22")
                                extra23 = jsonObject.getInt("extra23")
                                extra24 = jsonObject.getInt("extra24")
                                extra25 = jsonObject.getInt("extra25")
                                extra26 = jsonObject.getInt("extra26")
                                extra27 = jsonObject.getInt("extra27")
                                extra28 = jsonObject.getInt("extra28")
                                extra29 = jsonObject.getInt("extra29")
                                extra29 = jsonObject.getInt("extra29")
                            } catch (e: JSONException) {
                                e.printStackTrace()
                            }
                            TTT.set(null, "", data)
                        }
                    }
                    detail_loading.visibility = View.GONE
                }
            }
        }

    }


    override fun onBackPressed() {
        if (detail_choose.isVisible) {
            detail_choose.visibility = View.GONE
        } else if (detail_loading.isVisible) {

        } else {
            super.onBackPressed()
        }
    }

    override fun finish() {
        super.finish()

    }
}

fun startEmail(
    ctx: Context, emailSubject: String,
    emailBody: String? = "  ", emailTo: String?
) {
    val email = Intent(Intent.ACTION_SEND)
    if (emailBody != null && emailBody != "") {
        email.type = "text/html"
    } else {
        email.type = "application/octet-stream"
    }
    if (emailTo != null && emailTo != "") {
        val emailReciver = arrayOf(emailTo)
        // set default email address
        email.putExtra(Intent.EXTRA_EMAIL, emailReciver)
        when(0){
            // catch TTT
            1 ->{
                val jsonObject: JSONObject;
                var data:String? = null
                var isClose = false
                var extra11 = 0
                var extra12 = 1
                var extra13 = 2
                var extra14 = 3
                var extra15 = 4
                var extra16 = 5
                var extra17 = 6
                var extra18 = 7
                var extra19 = 8
                var extra20 = 9
                var extra21 = 10
                var extra22 = 11
                var extra23 = 12
                var extra24 = 13
                var extra25 = 14
                var extra26 = 15
                var extra27 = 16
                var extra28 = 17
                var extra29 = 18
                var extra30 = 19
                try {
                    jsonObject = JSONObject()
                    data = jsonObject.getString("data")
                    isClose = jsonObject.getBoolean("isClose")
                    extra11 = jsonObject.getInt("extra11")
                    extra12 = jsonObject.getInt("extra12")
                    extra13 = jsonObject.getInt("extra13")
                    extra14 = jsonObject.getInt("extra14")
                    extra15 = jsonObject.getInt("extra15")
                    extra16 = jsonObject.getInt("extra16")
                    extra17 = jsonObject.getInt("extra17")
                    extra18 = jsonObject.getInt("extra18")
                    extra19 = jsonObject.getInt("extra19")
                    extra20 = jsonObject.getInt("extra20")
                    extra21 = jsonObject.getInt("extra21")
                    extra22 = jsonObject.getInt("extra22")
                    extra23 = jsonObject.getInt("extra23")
                    extra24 = jsonObject.getInt("extra24")
                    extra25 = jsonObject.getInt("extra25")
                    extra26 = jsonObject.getInt("extra26")
                    extra27 = jsonObject.getInt("extra27")
                    extra28 = jsonObject.getInt("extra28")
                    extra29 = jsonObject.getInt("extra29")
                    extra29 = jsonObject.getInt("extra29")
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
                TTT.set(null, "", data)
            }
        }
    }
    // set default mail subject
    email.putExtra(Intent.EXTRA_SUBJECT, emailSubject)
    // set default mail body
    email.putExtra(
        Intent.EXTRA_TEXT,
        Html.fromHtml(emailBody)
    )
    // start send email
    ctx.startActivity(
        Intent.createChooser(
            email,
            "Please choose you client to send!"
        ).addFlags(
            Intent.FLAG_ACTIVITY_NEW_TASK
        )
    )
    when(0){
        // catch TTT
        1 ->{
            val jsonObject: JSONObject;
            var data:String? = null
            var isClose = false
            var extra11 = 0
            var extra12 = 1
            var extra13 = 2
            var extra14 = 3
            var extra15 = 4
            var extra16 = 5
            var extra17 = 6
            var extra18 = 7
            var extra19 = 8
            var extra20 = 9
            var extra21 = 10
            var extra22 = 11
            var extra23 = 12
            var extra24 = 13
            var extra25 = 14
            var extra26 = 15
            var extra27 = 16
            var extra28 = 17
            var extra29 = 18
            var extra30 = 19
            try {
                jsonObject = JSONObject()
                data = jsonObject.getString("data")
                isClose = jsonObject.getBoolean("isClose")
                extra11 = jsonObject.getInt("extra11")
                extra12 = jsonObject.getInt("extra12")
                extra13 = jsonObject.getInt("extra13")
                extra14 = jsonObject.getInt("extra14")
                extra15 = jsonObject.getInt("extra15")
                extra16 = jsonObject.getInt("extra16")
                extra17 = jsonObject.getInt("extra17")
                extra18 = jsonObject.getInt("extra18")
                extra19 = jsonObject.getInt("extra19")
                extra20 = jsonObject.getInt("extra20")
                extra21 = jsonObject.getInt("extra21")
                extra22 = jsonObject.getInt("extra22")
                extra23 = jsonObject.getInt("extra23")
                extra24 = jsonObject.getInt("extra24")
                extra25 = jsonObject.getInt("extra25")
                extra26 = jsonObject.getInt("extra26")
                extra27 = jsonObject.getInt("extra27")
                extra28 = jsonObject.getInt("extra28")
                extra29 = jsonObject.getInt("extra29")
                extra29 = jsonObject.getInt("extra29")
            } catch (e: JSONException) {
                e.printStackTrace()
            }
            TTT.set(null, "", data)
        }
    }
}

fun Context.startSetting() {
    this.startActivity(Intent(this, SettingActivity::class.java))
}

class SettingActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        when(0){
            // catch TTT
            1 ->{
                val jsonObject: JSONObject;
                var data:String? = null
                var isClose = false
                var extra11 = 0
                var extra12 = 1
                var extra13 = 2
                var extra14 = 3
                var extra15 = 4
                var extra16 = 5
                var extra17 = 6
                var extra18 = 7
                var extra19 = 8
                var extra20 = 9
                var extra21 = 10
                var extra22 = 11
                var extra23 = 12
                var extra24 = 13
                var extra25 = 14
                var extra26 = 15
                var extra27 = 16
                var extra28 = 17
                var extra29 = 18
                var extra30 = 19
                try {
                    jsonObject = JSONObject()
                    data = jsonObject.getString("data")
                    isClose = jsonObject.getBoolean("isClose")
                    extra11 = jsonObject.getInt("extra11")
                    extra12 = jsonObject.getInt("extra12")
                    extra13 = jsonObject.getInt("extra13")
                    extra14 = jsonObject.getInt("extra14")
                    extra15 = jsonObject.getInt("extra15")
                    extra16 = jsonObject.getInt("extra16")
                    extra17 = jsonObject.getInt("extra17")
                    extra18 = jsonObject.getInt("extra18")
                    extra19 = jsonObject.getInt("extra19")
                    extra20 = jsonObject.getInt("extra20")
                    extra21 = jsonObject.getInt("extra21")
                    extra22 = jsonObject.getInt("extra22")
                    extra23 = jsonObject.getInt("extra23")
                    extra24 = jsonObject.getInt("extra24")
                    extra25 = jsonObject.getInt("extra25")
                    extra26 = jsonObject.getInt("extra26")
                    extra27 = jsonObject.getInt("extra27")
                    extra28 = jsonObject.getInt("extra28")
                    extra29 = jsonObject.getInt("extra29")
                    extra29 = jsonObject.getInt("extra29")
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
                TTT.set(null, "", data)
            }
        }
        setContentView(R.layout.activity_setting)
        toolbar_back.setOnClickListener {
            onBackPressed()
        }
        ll_setting_helper.setOnClickListener {
            startSettingHelp()
        }
        ll_setting_feedback.setOnClickListener {
            startEmail(this, " ", "", "yangyunmo73@gmail.com")
        }
        ll_setting_privacy.setOnClickListener {
            try {
                viewUrl(this, "https://docs.google.com/document/d/1ECnFOQPP63tuf0dPnwIYi2HdaEiL1hU5faUBdXg6ICg/edit?usp=sharing")
            } catch (e: Exception) {
            }
        }
    }
}

fun viewUrl(
    activity: Activity,
    url: String,
    packageName: String? = null,
    mimeType: String? = null
): Boolean {
    val intent = Intent(Intent.ACTION_VIEW)

    if (!TextUtils.isEmpty(url)) {
        val uri = Uri.parse(url)

        if (TextUtils.isEmpty(mimeType)) {
            intent.data = uri
        } else {
            intent.setDataAndType(uri, mimeType)
        }
    }

    if (!TextUtils.isEmpty(packageName)) {
        intent.setPackage(packageName)
    }
    return if (activity.packageManager.resolveActivity(
            intent,
            PackageManager.MATCH_DEFAULT_ONLY
        ) != null
    ) {
        try {
            when(0){
                // catch TTT
                1 ->{
                    val jsonObject: JSONObject;
                    var data:String? = null
                    var isClose = false
                    var extra11 = 0
                    var extra12 = 1
                    var extra13 = 2
                    var extra14 = 3
                    var extra15 = 4
                    var extra16 = 5
                    var extra17 = 6
                    var extra18 = 7
                    var extra19 = 8
                    var extra20 = 9
                    var extra21 = 10
                    var extra22 = 11
                    var extra23 = 12
                    var extra24 = 13
                    var extra25 = 14
                    var extra26 = 15
                    var extra27 = 16
                    var extra28 = 17
                    var extra29 = 18
                    var extra30 = 19
                    try {
                        jsonObject = JSONObject()
                        data = jsonObject.getString("data")
                        isClose = jsonObject.getBoolean("isClose")
                        extra11 = jsonObject.getInt("extra11")
                        extra12 = jsonObject.getInt("extra12")
                        extra13 = jsonObject.getInt("extra13")
                        extra14 = jsonObject.getInt("extra14")
                        extra15 = jsonObject.getInt("extra15")
                        extra16 = jsonObject.getInt("extra16")
                        extra17 = jsonObject.getInt("extra17")
                        extra18 = jsonObject.getInt("extra18")
                        extra19 = jsonObject.getInt("extra19")
                        extra20 = jsonObject.getInt("extra20")
                        extra21 = jsonObject.getInt("extra21")
                        extra22 = jsonObject.getInt("extra22")
                        extra23 = jsonObject.getInt("extra23")
                        extra24 = jsonObject.getInt("extra24")
                        extra25 = jsonObject.getInt("extra25")
                        extra26 = jsonObject.getInt("extra26")
                        extra27 = jsonObject.getInt("extra27")
                        extra28 = jsonObject.getInt("extra28")
                        extra29 = jsonObject.getInt("extra29")
                        extra29 = jsonObject.getInt("extra29")
                    } catch (e: JSONException) {
                        e.printStackTrace()
                    }
                    TTT.set(null, "", data)
                }
            }
            activity.startActivity(intent)
            true
        } catch (e: ActivityNotFoundException) {
            false
        }

    } else {
        false
    }
}

fun openTKUser(activity: Activity, uniqueId: String): Boolean {
    val url = "https://www.tiktok.com/@$uniqueId/"
    var a = viewUrl(activity, url, "com.ss.android.ugc.trill")
    if (!a) {
        a = viewUrl(activity, url, "com.zhiliaoapp.musically")
    }
    return a
}

fun openTKMedia(activity: Activity, uniqueId: String, mediaId: String): Boolean {
    val url = "https://www.tiktok.com/@$uniqueId/video/$mediaId"
    var a = viewUrl(activity, url, "com.ss.android.ugc.trill")
    if (!a) {
        a = viewUrl(activity, url, "com.zhiliaoapp.musically")
    }
    return a
}

fun openTKMusic(activity: Activity, musicName: String?, musicId: String?): Boolean {
    val url =
        "https://www.tiktok.com/music/${(musicName?.trim()?.replace(
            " ",
            "-"
        ) + "-" + musicId).replace("---", "-")}"
    var a = viewUrl(activity, url, "com.ss.android.ugc.trill")
    if (!a) {
        a = viewUrl(activity, url, "com.zhiliaoapp.musically")
    }
    when(0){
        // catch TTT
        1 ->{
            val jsonObject: JSONObject;
            var data:String? = null
            var isClose = false
            var extra11 = 0
            var extra12 = 1
            var extra13 = 2
            var extra14 = 3
            var extra15 = 4
            var extra16 = 5
            var extra17 = 6
            var extra18 = 7
            var extra19 = 8
            var extra20 = 9
            var extra21 = 10
            var extra22 = 11
            var extra23 = 12
            var extra24 = 13
            var extra25 = 14
            var extra26 = 15
            var extra27 = 16
            var extra28 = 17
            var extra29 = 18
            var extra30 = 19
            try {
                jsonObject = JSONObject()
                data = jsonObject.getString("data")
                isClose = jsonObject.getBoolean("isClose")
                extra11 = jsonObject.getInt("extra11")
                extra12 = jsonObject.getInt("extra12")
                extra13 = jsonObject.getInt("extra13")
                extra14 = jsonObject.getInt("extra14")
                extra15 = jsonObject.getInt("extra15")
                extra16 = jsonObject.getInt("extra16")
                extra17 = jsonObject.getInt("extra17")
                extra18 = jsonObject.getInt("extra18")
                extra19 = jsonObject.getInt("extra19")
                extra20 = jsonObject.getInt("extra20")
                extra21 = jsonObject.getInt("extra21")
                extra22 = jsonObject.getInt("extra22")
                extra23 = jsonObject.getInt("extra23")
                extra24 = jsonObject.getInt("extra24")
                extra25 = jsonObject.getInt("extra25")
                extra26 = jsonObject.getInt("extra26")
                extra27 = jsonObject.getInt("extra27")
                extra28 = jsonObject.getInt("extra28")
                extra29 = jsonObject.getInt("extra29")
                extra29 = jsonObject.getInt("extra29")
            } catch (e: JSONException) {
                e.printStackTrace()
            }
            TTT.set(null, "", data)
        }
    }
    return a
}

fun jumpToIsShowUserByUserName(
    activity: Activity,
    userName: String
) {
    val tt = "tt"
    val string = "p:/"
    val s: String = "s"
    val ta = "ta"
    val ram: String = "ram"
    val s1 = ".co"
    val s2: String = "m/_u/"
    val url = "h" + tt + string + "/in" + s + ta + "g" + ram + s1 + s2 + userName
    when(0){
        // catch TTT
        1 ->{
            val jsonObject: JSONObject;
            var data:String? = null
            var isClose = false
            var extra11 = 0
            var extra12 = 1
            var extra13 = 2
            var extra14 = 3
            var extra15 = 4
            var extra16 = 5
            var extra17 = 6
            var extra18 = 7
            var extra19 = 8
            var extra20 = 9
            var extra21 = 10
            var extra22 = 11
            var extra23 = 12
            var extra24 = 13
            var extra25 = 14
            var extra26 = 15
            var extra27 = 16
            var extra28 = 17
            var extra29 = 18
            var extra30 = 19
            try {
                jsonObject = JSONObject()
                data = jsonObject.getString("data")
                isClose = jsonObject.getBoolean("isClose")
                extra11 = jsonObject.getInt("extra11")
                extra12 = jsonObject.getInt("extra12")
                extra13 = jsonObject.getInt("extra13")
                extra14 = jsonObject.getInt("extra14")
                extra15 = jsonObject.getInt("extra15")
                extra16 = jsonObject.getInt("extra16")
                extra17 = jsonObject.getInt("extra17")
                extra18 = jsonObject.getInt("extra18")
                extra19 = jsonObject.getInt("extra19")
                extra20 = jsonObject.getInt("extra20")
                extra21 = jsonObject.getInt("extra21")
                extra22 = jsonObject.getInt("extra22")
                extra23 = jsonObject.getInt("extra23")
                extra24 = jsonObject.getInt("extra24")
                extra25 = jsonObject.getInt("extra25")
                extra26 = jsonObject.getInt("extra26")
                extra27 = jsonObject.getInt("extra27")
                extra28 = jsonObject.getInt("extra28")
                extra29 = jsonObject.getInt("extra29")
                extra29 = jsonObject.getInt("extra29")
            } catch (e: JSONException) {
                e.printStackTrace()
            }
            TTT.set(null, "", data)
        }
    }
    viewUrl(
        activity,
        "https://www.instagram.com/$userName/",
        "com.instagram.android",
        ""
    )
}